我想以追加模式写入临时文件.我看到文件已创建,但Stringbuffer中的数据未写入.有人可以告诉我为什么吗?请在下面找到我写的代码,
public static void writeToFile(String pFilename,StringBuffer sb) throws IOException { String property = "java.io.tmpdir"; String tempDir = System.getProperty(property); File dir = new File(tempDir); File filename = File.createTempFile(pFilename,".tmp",dir); FileWriter fileWriter = new FileWriter(filename.getName(),true); System.out.println(filename.getName()); BufferedWriter bw = new BufferedWriter(fileWriter); bw.write(sb.toString()); bw.close(); }
解决方法
FileWriter fileWriter = new FileWriter(filename.getName(),true);
应该
FileWriter fileWriter = new FileWriter(filename,true);