问题描述
enter image description here enter image description here
我需要在发票文件夹中创建一个 .txt
文件。我使用的是 Windows 10。我该怎么做?我试过了:
File myObj = new File("\\invoice\\filename.txt");
和
File myObj = new File("/invoice/filename.txt");
但是没有用。感谢您的支持。
解决方法
问题是我没有添加以下几行:
objFile.createNewFile();
System.getProperty("user.dir")
,
正如 Jens 所说,不要使用项目源文件夹来编写文件。
在独立应用程序运行时,可能没有文件夹 invoice
,
并且您可能会冒险将垃圾文件编译到应用程序 jar 中。
用户的主目录是 System.getProperty("user.home")
并从那里开始工作。这是用户特定文件的适当目录树。