英文:
File Exists() method finds file that does not exist in Eclipse Java Package
问题
请查看下面的代码。我正在使用Eclipse工作。我正在处理的项目明确没有一个名为"log.txt"的附加文件。但是当我运行下面的代码时,"Exists"值会被打印到控制台。可能是什么原因导致这种情况?
File f = new File("log.txt");
if (f.exists()) {
System.out.println("Exists");
} else {
System.out.println("不存在");
}
英文:
Please see my code below. I'm working in Eclipse. The project I'm working on definitely does not have an attached file called "log.txt". But when I run the code below, the value "Exists" is printed to the console. What could be driving this?
File f = new File("log.txt");
if(f.exists()) {
System.out.println("Exists");
} else {
System.out.println(" Doesnt Exist");
}
答案1
得分: 2
相对路径,比如"log.txt",会根据用户的"当前工作目录"解析,这取决于应用程序的启动方式。应用程序可以在文件系统的任何位置查找log.txt。
如果.exists
返回true,表示文件存在。打印绝对文件路径以查看文件所在位置:
System.out.println(f.getAbsoluteFile() + " 存在");
英文:
Relative paths such as "log.txt" are resolved against the user's "current working directory" which depends on how the application is started. The application could be looking for log.txt anywhere on the file system.
If .exists
returns true, the file exists. Print the absolute file path to see where the file is located:
System.out.println(f.getAbsoluteFile() + " Exists");
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论