File Copying FileReader FileWriter

We have seen earlier, clearly the style and basics of file copying in Java, with byte streams FileInputStream and FileOutputStream in "Semantics of File Copying" and the it&339;s drawbacks of file copying in "Performance drawbacks of file copying".

Now let us do the same job but with character streams – FileReader and FileWriter. The same previous program "FileToFile1.java" is modified to suit character streams. The same rules and explanation works here also.

Following program does File Copying FileReader FileWriter to copy file to file

.

import java.io.*;
public class FRFWFileCopy
{
  public static void main(String args[])
  {
    try
    {  
      FileReader fr = new FileReader("pqr.txt");
      FileWriter fw = new FileWriter("xyz.txt");
     
      int k;
      while( ( k = fr.read() ) != -1 )
      {
        fw.write(k);   		
        System.out.print((char) k);  
      }
      fw.close();
      fr.close();
    }
    catch(FileNotFoundException e)
    {
      System.out.println("File does not exist. " + e);
    }
    catch(IOException e)
    {
      System.out.println("Some I/O problem. " + e);
    }
  }
}

File Copying FileReader FileWriter

Output screenshot on File Copying FileReader FileWriter

FileReader fr = new FileReader(“pqr.txt”);
FileWriter fw = new FileWriter(“xyz.txt”);

FileReader, being a reader class, opens the file "pqr.txt" in read mode and similarly FileWriter, being a writer class, opens the file "xyz.txt" in write mode.

k = fr.read()
fw.write(k);

read() method of FileReader reads one byte, at a time, from he source file "pqr.txt", converts it into ASCII integer value and returns. For example, if the character A exists in the source file, it is read, converted to 65 and returned. The write() method of FileWriter takes the integer value, converts back to the original character A and then writes to the destination file "xyz.txt".

fw.close();
fr.close();

When the job is over, close the stream objects (handles), first writer and then reader for proper flushing of data.

Two catch blocks are included to handle the exceptions thrown by different methods shown below.

  1. FileReader constructor throws FileNotFoundException.
  2. FileWriter constructor throws FileNotFoundException.
  3. read() method throws IOException.
  4. write() method throws IOException.
  5. Both close() methods throw IOException.

Note: For more clear explanation, it is advised to read FileToFile1.java program before this.

2 thoughts on “File Copying FileReader FileWriter”

  1. Hello sir.
    i have one doubt on character Streams and Byte Streams.i saw the two programs of file copying.
    one with fileInputStraem and another one with FileReader.both are reading one byte at a time.if we print the int value in while loop , we are getting same output in bothprogram.Can you explain difference between fileReader and fileinputstream through program?

    1. Character streams reads bytes and treat them as characters. For this reason, character streams cannot be used to read images. For images, only byte streams can be used.

Leave a Comment

Your email address will not be published.