我已成功使用PrintWriter將字符串寫入文本文件,並且默認情況下會將輸出文本文件寫入正在處理的Eclipse項目的目錄中。Printwriter目標問題
但我的Eclipse項目已要求我想要的文本文件寫入到資源的特定文件夾:
我的代碼是:
protected void saveCommandsToFile() throws FileNotFoundException, IOException {
PrintWriter out = new PrintWriter("commands.txt");
int listsize = list.getModel().getSize();
for (int i=0; i<listsize; i++){
Object item = list.getModel().getElementAt(i);
String command = (String)item;
System.out.println(command); //use console output for comparison
out.println(command);
}
out.close();
}
如果我行更改爲:
PrintWriter out = new PrintWriter("/Resources/commands.txt");
正在拋出FileNotFoundException。我如何解決這個問題?謝謝!