@JoetheDailyProgrammer,你可能写错了文件。试试这个代码:
public static void main(String[] args) throws IOException {
File out = new File("output.txt");
System.out.println(out.getAbsolutePath());
}
运行它,它会显示你的绝对路径"output.txt"
。如果指向到不同的位置,比你预期的,那么使用绝对路径在你的应用程序或使用用户主目录,如下所示:
public static void main(String[] args) throws IOException {
File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt"); // file into users homedir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
PrintWriter out = new PrintWriter(outFile); // writer over that file
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
out.print(filePath);
}
});
out.close();
}
}
UPD。
如果是真的你打算保存文件到桌面文件夹,那么很可能你应该浏览它就像在以下答案:
https://stackoverflow.com/a/1080900/2109067
https://stackoverflow.com/a/570536/2109067
所以:
public static void main(String[] args) throws IOException {
FileSystemView fsv = FileSystemView.getFileSystemView();
File outFile = new File(fsv.getHomeDirectory() + "/output.txt"); // file into desktop dir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
...
}
您是否尝试过打印控制台胀?有什么东西出现吗? –
是的,当我打印控制台时,一切都显示出来。 – Joe
你想用File.isRegularFile(Path p)完成什么?你在寻找一个不是目录的文件吗? – lacraig2