我正在製作一個程序,它涉及編輯文件並用路徑替換佔位符。getPath()不返回斜線? (Windows)
下面是代碼:
List<String> lines = Files.readAllLines(new File(new File(basepath, "Game"), "launcher_profiles.json").toPath());
int index = -1;
for (String s : lines){
index++;
if (s.contains("PROGRAM/GAMEDIRPATH")) break;
}
String k = lines.get(index);
k = k.replaceAll("PROGRAM/GAMEDIRPATH", new File(basepath, "Game").getPath());
lines.set(index, k);
clearFile(new File(new File(basepath, "Game"), "launcher_profiles.json"));
Files.write(new File(new File(basepath, "Game"), "launcher_profiles.json").toPath(), lines, StandardOpenOption.CREATE);
沒有與基本路徑沒有問題,「遊戲」文件的路徑,因爲在我的程序中所有其他的地方,它包括斜線。它只是在這部分代替不包括斜線的佔位符。
例如,C:/ Users \ name \ Documents \ program \ Game不是 ,而是返回UsersnameDocumentsprogramGame。
如前所述,在所有其他地方,它會返回正確的路徑名(帶有斜槓)。 在Mac上,即使在這部分中,所有斜槓都在那裏。
有誰知道一個修復?謝謝。
我想你想'k.replaceAll(「PROGRAM」+ File.separator +「GAMEDIRPATH」'加上顯示你的輸入,並澄清你的問題,如果不解決 –