File Copy Tutorial Java

File Copy Tutorial Java

File Copy Tutorial Java: File copying operation, in Java, can be done in 5 steps broadly.
  1. Open the source file in read mode (say, using FileInputStream constructor)
  2. Open the destination file in write mode (say, using FileOutputStream constructor)
  3. Read data from the source file (say, using read() method)
  4. Write data to the destination file (say, using write(int) method)
  5. When the job is over, close the streams (say, using close() method )

Above steps are illustrated in the next program

Copying file to file using low-level byte streams

The following program uses two byte streams to copy file to file – FileInputStream and FileOutputStream.

File Copy Tutorial Java
Output screen on File Copy Tutorial Java

FileInputStream fis = new FileInputStream("pqr.txt");
FileOutputStream fos = new FileOutputStream("xyz.txt");

In the above code, pqr.txt and xyz.txt are the source and destination files opened using the constructors of FileInputStream and FileOutputStream. FileInputStream constructor opens the file in read mode and FileOutputStream constructor opens the file in write mode. Both constructors throw a checked exception FileNotFoundException and is handled in the first catch block.

If the destination file (xyz.txt) does not exist, a new file is created and the source file (pqr.txt) contents are copied. If the destination file already exists, the old contents are overridden; that is, lost. To have an append mode, modify the above statement as follows.

FileOutputStream fos = new FileOutputStream("xyz.txt", true);

The second parameter true indicates append mode. Now, the old contents are appended with the new contents.;

The read() method of FileInputStream (inherited from its super class InputStream, see hierarchy) reads byte after byte from the source file, pqr.txt. It has got a very peculiar nature. It reads a byte, converts into its ASCII integer value and returns. For example, if the source file contains A, it converts into 65 and returns. Moreover, if EOF reaches while reading, this method returns -1. In the while loop, every byte, the read() method reads is checked whether EOF is reached or not by comparing with -1. When the read() method returns -1, the loop is terminated and further reading is stopped. Following is the method signature of read() method as defined in FileInputStream class.

public int read() throws IOException

As you can observe, the read method throws a checked exception, IOException which is caught in the second catch block.


The write(int) method of FileOutputStream takes the ASCII integer value returned by the read() method, converts into the original character and writes into the destination file, xyz.txt. Following is the method signature of write() method as defined in FileOutputStream class.

public void write(int) throws IOException

Like read() method, the write() method also throws the checked exception IOException and is caught in the same second catch block.

System.out.print((char) k);

To print at the DOS prompt, the integer value is converted into character and then written; else, ASCII values are printed.

fos.close(); fis.close();

As a good programming practice, after the usage of file streams, the file handles (objects) should be closed, that too, first output stream and then input stream. By doing so, certain operations are done implicitly by the JVM, given below.

  1. Data in the buffers, if exist, is cleared.
  2. File format is checked (like .txt, .doc and .gif etc).
  3. When the format is okay, the OS adds EOF marker.

When the EOF marker is added, the file definitely opens in the correct format, the way it is saved, even after 10 years.

Following is the method signature of close() method as defined in FileInputStream and FileOutputStream classes.

public void close() throws IOException

2 thoughts on “File Copy Tutorial Java

Leave a Reply

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