How to write Text File in Java by BufferedWriter

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

I. BufferedWriter – Classis Approach (Java5, Java6)

Now C:\\test\\test.txt file with content:

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))

Content of C:\\test\\test.txt file:

II. BufferedWriter – Java 7 Approach

Implement With JAVA 7: use try-catch-resource
-> No need to CLOSE the buferedWriter after processing.

Code for Append content:

III. Sourcecode

Write2FileJava


Related Posts



Got Something To Say:

Your email address will not be published. Required fields are marked *

*