1. 程式人生 > >在Windows下的路徑分隔符和Linux下的路徑分隔符是不一樣的

在Windows下的路徑分隔符和Linux下的路徑分隔符是不一樣的

比如說要在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");
 

String zip = savePath + File.separator + Common
.QRCODE + File.separator + subpath +File.separator + batchCode + Common.ZIP;

windows linux 都寫成 /temp 這樣的也是可以的  謝謝 yanek  的告知