java 路径分隔符

路径分隔符:
Window:反斜杠“\”
Linux:正斜杠“/”
1、考虑到程序的可移植性,在Linux和Window下最好都要用“/”,都是可以的。
2、用File.operator

File f = new File("d://test//1.txt");   √
File f = new File("d:/test/1.txt");√
File f = new File("d:\\test\\1.txt");√
File f = new File("d:\test\1.txt");  Χ



匹配文件
Window \\
Linux  /

比如:HashMap<String,double> key-value
key代表文件路径 value代表文件的值 。如果要匹配key的话:

String str="d:/book/1.txt";
System.out.println(str.contains("\\1.txt"));//Window下匹配,输出true
System.out.println(str.contains("/1.txt"));//Linux下匹配,输出true




猜你喜欢

转载自420532394.iteye.com/blog/2223697
今日推荐