我已成功使用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。我如何解决这个问题?谢谢!