下面的行和注释行产生相同的结果:
public class StringEscapeMain {
public static void main(String[] args){
String fileName = "C:\\Dev\\TryEclipseJava\\StringEscape\\\\\\f.txt";
/* String fileName = "C:\\Dev\\TryEclipseJava\\StringEscape\\f.txt";*/
File file = new File(fileName);
if(file.exists()){
System.out.println("File exists!");
}
else{
System.out.println("File does not exist!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Java总是将任何超过2个斜杠的斜杠序列视为与"\"相同吗?
谢谢!
第\一种用于逃避,这意味着
C:\\Dev\\TryEclipseJava\\StringEscape\\\\\\f.txt
将被编译为
C:\Dev\TryEclipseJava\StringEscape\\\f.txt.
和
C:\\Dev\\TryEclipseJava\\StringEscape\\f.txt
将被编译为
C:\Dev\TryEclipseJava\StringEscape\f.txt.
使用以下命令创建File实例时:
File file = new File(fileName);
Run Code Online (Sandbox Code Playgroud)
该fileName将"正常化"根据您的FileSystem:
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
Run Code Online (Sandbox Code Playgroud)
在"正常化"程序中WinNTFileSystem,
C:\Dev\TryEclipseJava\StringEscape\\\f.txt
将被截断为:
C:\Dev\TryEclipseJava\StringEscape
然后它会:
从路径的其余部分删除冗余斜杠,强制所有斜杠进入首选斜杠
最后,fileName归一化为:
C:\Dev\TryEclipseJava\StringEscape\f.txt.
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |