Java provides a BufferedWriter for writting content to File with high performance when performing with large number of write action. Because BufferedWriter decreases I/O interaction by using internal buffer to write data into File.
Related posts:
1. Java Read Text File by BufferedReader, Java 7,Java 8
2. How to delete non-empty Folder in Java
Contents
I. BufferedWriter – Classis Approach (Java5, Java6)
String FILE_NAME = "C:\\test\\test.txt"; try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME)); StringBuffer content = new StringBuffer(); content.append("Line 1"); content.append(System.getProperty("line.separator")); content.append("Line 2"); content.append(System.getProperty("line.separator")); content.append("Line 3"); content.append(System.getProperty("line.separator")); content.append("Line 4"); content.append(System.getProperty("line.separator")); content.append("Line 5"); content.append(System.getProperty("line.separator")); // write content bufferedWriter.write(content.toString()); // close BufferedWriter bufferedWriter.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } |
Now C:\\test\\test.txt file with content:
Line 1 Line 2 Line 3 Line 4 Line 5 |
For append data to an existed File, In constructor: public FileWriter(String fileName, boolean append)
set append = true
new BufferedWriter(new FileWriter(FILE_NAME,true))
try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME,true)); StringBuffer content = new StringBuffer(); content.append("Line 6"); content.append(System.getProperty("line.separator")); content.append("Line 7"); content.append(System.getProperty("line.separator")); content.append("Line 8"); content.append(System.getProperty("line.separator")); content.append("Line 9"); content.append(System.getProperty("line.separator")); content.append("Line 10"); content.append(System.getProperty("line.separator")); // write content bufferedWriter.write(content.toString()); // close BufferedWriter bufferedWriter.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } |
Content of C:\\test\\test.txt file:
Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 |
II. BufferedWriter – Java 7 Approach
Implement With JAVA 7: use try-catch-resource
-> No need to CLOSE the buferedWriter after processing.
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME))){ StringBuffer content = new StringBuffer(); content.append("Line 1"); content.append(System.getProperty("line.separator")); content.append("Line 2"); content.append(System.getProperty("line.separator")); content.append("Line 3"); content.append(System.getProperty("line.separator")); content.append("Line 4"); content.append(System.getProperty("line.separator")); content.append("Line 5"); content.append(System.getProperty("line.separator")); bufferedWriter.write(content.toString()); System.out.println("Done"); }catch(IOException e){ e.printStackTrace(); } |
Code for Append content:
// // Append content to a FILE using BufferedWriter // In constructor: public FileWriter(String fileName, boolean append) // -> set append = true // try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME, true))){ StringBuffer content = new StringBuffer(); content.append("Line 6"); content.append(System.getProperty("line.separator")); content.append("Line 7"); content.append(System.getProperty("line.separator")); content.append("Line 8"); content.append(System.getProperty("line.separator")); content.append("Line 9"); content.append(System.getProperty("line.separator")); content.append("Line 10"); content.append(System.getProperty("line.separator")); bufferedWriter.write(content.toString()); System.out.println("Done"); }catch(IOException e){ e.printStackTrace(); } |
III. Sourcecode
Last updated on June 4, 2017.