在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

 

比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File ("C:\tmp\test.txt");
在Linux下则是这样的:
File file2 = new File ("/tmp/test.txt");

如果要考虑跨平台,则最好是这么写:
File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");

相关文章:

  • 2022-12-23
  • 2021-07-15
  • 2021-05-12
  • 2021-12-18
猜你喜欢
  • 2021-11-09
  • 2022-12-23
  • 2021-07-19
  • 2021-07-03
  • 2022-12-23
  • 2021-11-11
相关资源
相似解决方案