Answer to Question #253215 in Java | JSP | JSF for Tarurendra Kushwah

Question #253215

Write a program to copy from one file to another.


1
Expert's answer
2021-10-21T05:21:14-0400
package com.company;

import java.util.*;

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

public class Main
{
    public static void main(String[] args)
    {
        FileInputStream input_stream = null;
        FileOutputStream output_stream = null;

        try{
            //give the path name of your input and output files
            File input_file =new File("C:\\IdeaProjects\\LEarn_java\\src\\com\\company\\kelly.txt");
            File output_file =new File("C:\\IdeaProjects\\LEarn_java\\src\\com\\company\\copy.txt");

            input_stream = new FileInputStream(input_file);
            output_stream = new FileOutputStream(output_file);

            byte[] buffer = new byte[1024];

            int length;
            /*copying the contents from input stream to
             * output stream using read and write methods
             */
            while ((length = input_stream.read(buffer)) > 0){
                output_stream.write(buffer, 0, length);
            }

            //Closing the input/output file streams
            input_stream.close();
            output_stream.close();

            System.out.println("File copied successfully!!");

        }catch(IOException ioe){
            ioe.printStackTrace();
        }
    }
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS