简体   繁体   English

Java中的File.exists()-相对还是绝对路径?

[英]File.exists() in Java - Relative or Absolute path?

I cannot find this simple piece of information in the Javadoc anywhere, or anywhere else for the matter. 我在Javadoc的任何地方或其他地方都找不到这种简单的信息。

Let's say I have a file in the path C:\\users\\userOne\\myFile.exe 假设我在路径C:\\ users \\ userOne \\ myFile.exe中有一个文件

And I create a File instance with the above directory 我用上面的目录创建一个File实例

String myFile = "myFile.exe";
File file = new File("C:\users\userOne\" + myFile);

Do both functions below return true? 下面的两个函数都返回true吗?

file.exists("C:\" + myFile);
file.exists("C:\users\userOne\" + myFile);

The answer is both. 答案是两者兼而有之。 If it is an absolute path, then it will use absolute path. 如果它是绝对路径,那么它将使用绝对路径。 If the path is relative, then it is relative. 如果路径是相对的,则它是相对的。 I'm not sure what else to tell you? 我不确定还有什么要告诉你的?

By the way, your first example won't work because c:\\myFile.exe doesn't exist. 顺便说一句,您的第一个示例将不起作用,因为c:\\ myFile.exe不存在。 Also, as pointed out, that method doesn't take parameters. 同样,如前所述,该方法不带参数。

// current working directory
System.getProperty("user.dir")

I understood the answer, you are looking for : In Java, We have to escaping important while pointing to some path. 我了解了您要寻找的答案:在Java中,当指向某些路径时,我们必须逃避重要的操作。

\\\\ is used for escaping the backslash. \\\\用于转义反斜杠。

Try this, you should be good: 试试这个,你应该很好:

C:\\users\\userOne\\" + myFile C:\\ users \\ userOne \\“ + myFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM