如何解决java.io.FileNotFoundException: hi.txt系统找不到指定的文件在 java.base/java.io.FileOutputStream.open0(Native Method)
所以基本上我一直在编译这个创建新 .txt 文件并向其中写入内容的简单代码。不知道如何处理这个系统找不到指定的文件问题。
这是我的代码:
import java.io.File;
import java.io.FileWriter;
public class CreateANewFile {
public static void main(String[] args) {
File file = new File("hi.txt");
if (!file.isFile()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
FileWriter writer;
try {
writer = new FileWriter(file);
writer.write("null");
writer.close();
System.out.println("done!! check if the file has created or not.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是错误:
java.io.IOException: The system cannot find the file specified
at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.base/java.io.File.createNewFile(File.java:1024)
at CreateANewFile.main(CreateANewFile.java:10)
java.io.FileNotFoundException: hi.txt (The system cannot find the file specified)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:185)
at java.base/java.io.FileWriter.<init>(FileWriter.java:96)
at CreateANewFile.main(CreateANewFile.java:17)
所以基本上,JVM 不被允许创建或写入任何文件。
我已经交叉检查了所有环境变量的路径是否正确。
怎么了?? 如何解决问题?
提前致谢。 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。