文件路径的分隔符在windows系统和linux系统中是不一样。
比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File (“C:\tmp\test.txt”);
在Linux下则是这样的:
File file2 = new File (“/tmp/test.txt”);
而我刚开始就是按照File file1 = new File (“C:\tmp\test.txt”);这种方式写的,在
Windows下没有问题,但是将工程部署在服务器上时,就出问题了。服务器是linux系统,所
以这时文件路径就出错了。后来将分隔符用File.separator 代替,ok,问题解决了。下边介
绍下File.separator 。
如果要考虑跨平台,则最好是这么写:
File myFile = new File(“C:” + File.separator + “tmp” + File.separator, “test.txt”);
比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File (“C:\tmp\test.txt”);
在Linux下则是这样的:
File file2 = new File (“/tmp/test.txt”);
而我刚开始就是按照File file1 = new File (“C:\tmp\test.txt”);这种方式写的,在
Windows下没有问题,但是将工程部署在服务器上时,就出问题了。服务器是linux系统,所
以这时文件路径就出错了。后来将分隔符用File.separator 代替,ok,问题解决了。下边介
绍下File.separator 。
如果要考虑跨平台,则最好是这么写:
File myFile = new File(“C:” + File.separator + “tmp” + File.separator, “test.txt”);
File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。