Java Program to copy a file

Write a Java Program to copy a file :

In this tutorial, we will learn how to copy a file in java . In the example explained below , we are showing one ‘pdf’ file to copy and create a different file in the same location.

Here, we are using ’FileInputStream’ to read contents of a file. ’FileInputStream’ reads the contents byte wise and returns an int value containing that byte, after it complete the reading, the return value will become -1. So, we have one ‘while’ loop that checks if the reading byte is ‘-1’ or not. If not, keep reading the data.

But, we need to keep writing the reading bytes simultaneously. For this task, we have created one ’FileOutputStream’ object. Inside the while loop, we are writing the data to this object. ’FileOutputStream’ is a output stream to write contents to a file.

After the ‘while’ loop will complete, i.e. reading and writing data both will complete , the program will exit. You will get a new file at the location provided for ’FileOutputStream’. This same program can be used to copy any file type like image,music etc.

Example Program :

/*
 * Copyright (C) 2017 codevscolor.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

/**
 * Example class
 */
public class ExampleClass {

    //utility method to print a string
    static void print(String value) {
        System.out.println(value);
    }

    /**
     * Mehtod to copy a file using FileInputStream and FileOutputStream
     *
     * @throws IOException : This exception is thrown if any problem is found on closing the stream
     */
    private static void copyFile() throws IOException {

        //file is the input file and 'outputFile' is the newly copied file to create
        File file = new File("E://ticket.pdf");
        File outputFile = new File("E://ticket-copy.pdf");

        //create FileInputStream,fileOutputStream variables with initial value as null
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream(file); //link the FileInputStream with the input file
            fileOutputStream = new FileOutputStream(outputFile); //link the FileOutputStream with the output file

            //print the number of bytes to read for this stream
            print("Approx " + fileInputStream.available() + "bytes" + " to read");

            int i = 0; //this int value will hold the reading byte

            while ((i = fileInputStream.read()) != -1) {
                //write the reading byte on FileOutputStream
                fileOutputStream.write(i);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            //close both streams
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null){
                fileOutputStream.close();
            }
        }
    }


    public static void main(String[] args) {
        try {
            //calling method to copy a file
            copyFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Similar tutorials :