File Exists()方法在Eclipse Java Package中查找不存在的文件。

huangapple go评论81阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年7月31日 10:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63185035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定