Program to Copy data of one file into another file for 15 times

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class JavaCopyFile {

    public static void main(String[] args) throws InterruptedException, IOException {


        int i=0,count=0;;
        while(i<15) {
        File source = new File("error.txt");
        File dest = new File("criteria.txt");

        // copy file conventional way using Stream
        //long start = System.nanoTime();
        copyFileUsingStream(source, dest);
        //System.out.println("Time taken by Stream Copy = " + (System.nanoTime() - start));

        if(i<15) {

            count++;
        }
        i++;
    }
        System.out.println(count);
    }
    private static void copyFileUsingStream(File source, File dest)

            throws IOException {

        InputStream input = null;

        OutputStream output = null;

        try {

            input = new FileInputStream(source);

            output = new FileOutputStream(dest);

            byte[] buf = new byte[1024];

            int bytesRead;

            while ((bytesRead = input.read(buf)) > 0) {

                output.write(buf, 0, bytesRead);

            }

        }catch(Exception e) {
            System.out.println("File not found exception");/*finally {

            input.close();

            output.close();
*/
        }

    }

}

I have written code as above - for testing purpose I set count variable. count is giving 15 it is perfect. But file is copying once only. I want to copy the file for 15 times to the same destination file. Please help me to solve this problem. I am a beginner in java programming.

1 answer

  • answered 2018-11-08 06:31 Nicholas K

    As suggested in the comments, your logic is correct but you are re-writing the destination file every time.

    Use the variable i to denote each tiem you are writing to a new destination file as shown below :

    File dest = new File("criteria" + i + ".txt");
    

    This will create 15 files in the destination and thus avoids re-writing the same destination.


    EDIT :

    As per your latest comment change the code to as follows :

    public static void main(String[] args) throws InterruptedException, IOException {
        int i = 0, count = 0;
        File source = new File("error.txt");
        File dest = new File("criteria.txt");
        OutputStream output = new FileOutputStream(dest);
        while (i < 15) {
            copyFileUsingStream(source, output);
            if (i < 15) {
                count++;
            }
            i++;
        }
        System.out.println(count);
    }
    
    private static void copyFileUsingStream(File source, OutputStream output) 
      throws IOException {
        InputStream input = null;
        try {
            input = new FileInputStream(source);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } catch (Exception e) {
            System.out.println("File not found exception");
        }
    }
    

    Instead of creating the OutputStream in each iteration of the loop, create it once and pass it to copyFileUsingStream every time so that the previous stream is maintained.