File.separator是File类中的静态变量
在window环境下程序打印出来是:"\"
public class FileTest {
public static void main(String[] args) throws IOException {
File f = new File(new FileTest().getClass().getResource("").getPath());
System.out.println("File.separator:"+File.separator);
String pathTest = f + File.separator + "FileTest.java";
System.out.println(pathTest);
}
}
控制台输出:
File.separator:\
D:\day02\target\classes\com\zte\FileTest\FileTest.java
在Linux下的路径分隔符是不一样的是:"/"
例如程序中要创建个test.txt文件,在Windows下应该这么写:
File file1 = new File ("D:\filepath\test.txt");
在linux下则是这样的:
File file2 = new File ("/filepath/test.txt");
考虑跨平台,我们通常写:
File testFile = new File("D:" + File.separator + "filepath" + File.separator + "test.txt");