I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.
1. About BufferedWriter and BufferedReader
A buffer is a collective memory. Reader and Writer classes in java supports "Text Streaming". The "BufferedWriter" class of java supports writing a chain of characters output stream (Text based) in an efficient way. The Chain-Of-Characters can be Arrays, Strings etc. The "BufferedReader" class is used to read stream of text from a character based input stream.
The BufferedReader and BufferedWriter class provides support for writing and reading newline character. In windows ‘\r\n’ together forms the new line (Carriage return and Line Feed). But in Unix ‘\n’ is sufficient for a new line. With these "Buffered Text Stream" classes, we no need to worry about the platform while dealing with the Newline character.
The BufferedReader and Writer can be attached with other Reader and Writer classes for efficient streaming of the Data. In this example, we are going to overlap the FileWriter with BufferedWriter to perform the file writing. The same way, we are going to overlap BufferedReader over the FileReader. So, the net effect will be reading and writing a file with the newline character support without worrying about the underlying platform.
2. Write to a File using Java's BufferedWriter
The file reading and writing operation is error prone as it involves disc file. Say for example, there is no space in disc or the folder do not have permission to create files or the file does not exits etc. So first we need "IOException". First, we are going to write some text content to a file and to perform this we need FileWriter and BufferedWriter classes. The same-way for reading the file content, we need FileReader and BufferedReader classes. Below is the required package imports:
Now, look at the code below which writes some string contents to a Text file:
A FileWriter object fw is created and we are passing the file name with the path to its constructor (Marked as 1). Once we have the FileWriter object in hand, we are overlapping it with BufferedWriter. The BufferedWriter object WriteFileBuffer is created by passing the FileWriter object to its constructor (Marked as 2). We call overlapping one stream over another stream as "Stream Chaining".
Read More From Owlcation
The FileWriter object itself sufficient to write a text file. But, here we are overlapping it with a BufferedWriter to provide additional functionality of supporting the New Line characters. Also, the BufferedWriter minimizes the file-hit as it flushes the buffered content. Note that the text contents are written to the file TestFile.txt by calling the "write()" method (Marked as 3) . We are writing three lines of text and the "newline()" method is used to place platform specific new line character in the text file (Marked as 4). Finally, we are closing the Buffered Writer by calling the "close()" method (Marked as 5). Since the FileWriter is overlapped by the BufferedWriter, we no need to call the close() method on the FileWriter. Have a look at the below depiction:
Here, when we write our content to the buffered reader (Using write() and newLine() method), the reader makes use of the FileWriter to push text stream to a text file. The FileWriter knows writing the character to a text file. The BufferedWriter knows how to write it efficiently (by buffering the characters) and it takes care writing the new line character. Note that we make use of BufferedWriter to write the text content and BufferedWriter uses its underlying FileWriter.
3. Read from a File using Java's BufferedReader
In the previous section, we created a file using BufferedWriter. Now, we will read that TestFile.txt file and display the content of it in the console output window. To read the text file, we are going to use BufferedReader. Have a look at the code snippet below:
First, the java FileReader object fr is created. We are passing full path to the text file in the constructor (Marked as 1). Then, we are overlapping the FileReader with the BufferedReader by sending the FileReader object fr to the constructor of the BufferedReader. We are going to make all the read request to the BufferedReader object ReadFileBuffer (Marked as 2). Using the "readLine()" method of the BufferedReader, we are reading all three line of texts (Marked as 3). Note that the readLine() method reads the line of text along with the newline character. So, when we print the readLine() return string in the console output window, the cursor goes to next line after printing the line. Finally, we are closing both the Readers by calling the "close()" method on the BufferedReader object ReadFileBuffer (Marked as 4).
4. Full Code Example
Below is the complete code example:
Note: To run this example, make sure we have a folder called Temp in C:\ Root.