BufferedReader and BufferedWriter File Copy

BufferedReader and BufferedWriter File Copy

Speed up Copying

Reading and copying file to file is very slow with FileReader and FileWriter as for every byte read, the control should be shifted from the execution context area of source file to the destination file and back again to read the second byte. This is what you have done in Semantics of File Copying. This control shiftings (disk accesses) takes a long time than actual reading and writing (definitely, few thousand times more in case of a file of 2 lakhs bytes).

Performance increase with BufferedReader BufferedWriter

To increase the performance, let us decrease the control shiftings by putting buffers. The buffers are given by BufferedReader on reading-side and BufferedWriter on writing-side. Here, we use BufferedReader (equivalent to DataInputStream on byte sterams side) and BufferedWriter (equivalent to DataOutputStream).

The BufferedReader not only gives system-defined buffer but also comes with methods like readInt(), readDoube() and readLine() etc. that can read an integer, a double value and a line at a time. This reading also increases the performance. So, BufferedReader increases performance not only by decreasing the disk accesses with buffering but also while reading with methods readInt() and readLine() etc. Similarly, the BufferedWriter gives buffering affect and also writing affect with its methods writeInt(), writeDouble() and overloaded write() etc. that can write at a time an integer, a double value and a line.

Note: The readLine() method of DataInputStream is deprecated but not the readLine() method of BufferedReader. So, while using readLine() method of BufferedReader, we do not get deprecation warning at compilation time.

Following are the signatures of the classes BufferedReader BufferedWriter.

public class BufferedReader extends Reader
public class BufferedWriter extends Writer

Performance with Buffers using Character Streams

This program uses the methods readLine() and write() of BufferedReader and BufferedWriter.

BufferedReader BufferedWriter
Output screen on BufferedReader BufferedWriter

FileReader fr = new FileReader("Morals.txt");
BufferedReader br = new BufferedReader(fr);

For greater performance, the FileReader object fr is chained with BufferedReader.

The above two statements can be replaced with the following statement using anonymous objects to save memory.

BufferedReader br = new BufferedReader(new FileReader("Morals.txt"));

Similarly, the writing-side also.

BufferedWriter bw = new BufferedWriter(new FileWriter("xxx.txt"));

s1 = br.readLine() ) != null

The functionality of readLine() method of BufferedReader is the same as the readLine() method of DataInpuStream. In both, the readLine() method reads a line at a time and returns it as a string; irrespective of what the line contains; that may include digits and special characters. The method returns null if EOF marker is read.

bw.write(s1);
bw.newLine();

The write() method of BufferedWriter writes a line at a time to the destination file, xxx.txt. The newLine() method gives a new line in the destination file. The newLine() does not exist with byte streams. The new line functionality is achieved with DataOutputStream as follows.

String lineEnd = System.getProperty("line.separator");

This is where character streams are advantageous and easier over byte streams.

The write() method is overloaded where a few characters of string s1 can be written to the file.

bw.write(s1, 0, s1.length()/2);

The above statement writes half of the string s1 to the file, starting from offset 0 (that is, from the beginning of the line to the middle of the line).

Leave a Reply

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