【发布时间】:2014-03-06 19:04:36
【问题描述】:
我正在使用 PrintWriter 成功地将字符串写入文本文件,默认情况下,输出文本文件会写入我正在处理的 Eclipse 项目的目录中。
但我的 Eclipse 项目有一个名为 Resources 的特定文件夹,我希望将文本文件写入该文件夹:
我的代码是:
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。我该如何解决这个问题?谢谢!
【问题讨论】:
标签: java file-io printwriter