One, FileWritter writes files
FileWritter, character stream writes characters to files. By default it will replace all existing content with new content, however, when a true is specified (Boolean) value as the second argument to the FileWritter constructor, which retains the existing content and appends the new content at the end of the file.
1. Replace all existing content with new content.
new FileWriter(file);2. Keep the existing content and append the new content to the end of the file.
1 2 |
|
Append file example
A text file named "javaio-appendfile.txt" and containing the following content.
ABC Hello append new content new FileWriter(file,true)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Result
Now, the content of the text file "javaio-appendfile.txt" is updated as follows:
ABC Hello This content will append to the end of the file
Second, BufferedWriter writes files
Buffer characters (BufferedWriter
) is a character stream class to handle character data. Unlike byte streams (data converted into bytes), you can write string, array or character data directly to a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|