Hi all ,I came across the internet for such a method to write to a file,although I am taking java but never really learned about buffered writer instead I took Printstream class;my question is why the records of the file created using this method are not updated (if Ineed to add records) inside IntellijIdea,in stead you have always to check the file using Notepad,thanks for any reply here is the code:
Code:
public void writeToFile(String filename){
BufferedWriter bufferedWriter = null;
try {
// Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(new FileWriter(filename));
// Start writing to the output stream
bufferedWriter.write("Writing line one to file");
bufferedWriter.newLine();
bufferedWriter.write("Writing line two to file");
bufferedWriter.newLine();
bufferedWriter.write("Writing line three to file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// Close the BufferedWriter
try {
if (bufferedWriter != null){
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Comment