假设你的状态数组不为空,这段代码将工作,但该文件的文本文件将在编译/输出目录
所以在源目录中的文本文件,将不会更新更新,但输出目录中的那个将会。
还要注意的是FileWirter的构造函数,你正在使用将覆盖该文件的内容,所以你应该使用一个与append参数:
public FileWriter(String fileName, boolean append) throws IOException
编辑:如果你真的需要更新文件在src目录中,你可以像这样做。
不是真的不错,但这种将工作
public void updateF()throws Exception
{
String fileName = "valS.txt";
File fileInClasses = new File(getClass().getResource(fileName).getFile());
System.out.println(fileInClasses.getCanonicalPath());
File f = fileInClasses;
boolean outDir = false;
// let's find the output directory
while(!outDir){
f = f.getParentFile();
outDir = f.getName().equals("out");
}
// get the parent one more time
f = f.getParentFile();
// from there you should find back your file
String totoPath = f.getPath()+"/src/com/brol/" + fileName;
File totoFile = new File(totoPath);
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(totoFile, true));
outputWriter.append("test");
outputWriter.flush();
outputWriter.close();
}
有没有错误?异常? – Keerthivasan
没有更新文件:( – rick
我认为这行“getClass()。getResource(”valS.txt“)。getFile()”有一些问题 – rick