我有這個代碼,我創建一個文本文件,但正如你可以看到,路徑是硬編碼。在java上選擇路徑
如何設置用戶選擇路徑並將其傳遞給字符串的代碼,以便我可以將其放入保存txt的代碼中。
public void saveSudoku() {
try (
PrintStream savedSudoku = new PrintStream(new File("C:\\Users\\Daniel\\Desktop\\savedSudoku.txt"));) {
for (int i = 0; i < usedMatrix.length; i++) {
String sudokuNumber = "";
for (int j = 0; j < usedMatrix[i].length; j++) {
sudokuNumber += " " + userSudoku[i][j] + " ";
}
savedSudoku.println(sudokuNumber);
}
savedSudoku.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try (PrintStream levelAndHint = new PrintStream(new File("C:\\Users\\Daniel\\Desktop\\savedLevel.txt"));) {
levelAndHint.println(labelLevel.getText() + " " + hintCounter);
levelAndHint.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
這是一個獨立的Java應用程序? – sidgate
如果能解決您的問題,請接受答案 – sidgate