Write temporary files in Java
•
Java
I want to write temporary files in append mode I see that the file has been created, but the data in the StringBuffer has not been written Can someone tell me why? Please find the code I wrote below,
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(); }
Solution
FileWriter fileWriter = new FileWriter(filename.getName(),true);
FileWriter fileWriter = new FileWriter(filename.getName(),true);
should
FileWriter fileWriter = new FileWriter(filename,true);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码