Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the class code below and using only the main method, add code to make a

ID: 3690792 • Letter: C

Question

Complete the class code below and using only the main method, add code to make an EXACT COPY of the input file to an output file named copyOf_<input file>. For example, if we input the file named story.txt our class will make an exact copy of the file, but save it under the name copyOf_story.txt


public class CopyFile {

     public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter name of the file to copy FileName --> ");

Explanation / Answer

Create a file name story.txt and inserts some data in to any directory, here file story.txt placed in D: directory

story.txt

hai this is mike how r u

CopyFile.java

import java.io.File;//package for creating file
import java.io.FileInputStream;//for reading file
import java.io.FileOutputStream;//for writting file
import java.io.IOException;//package for IOEception
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile
{
public static void main(String[] args)
{//mian method

   InputStream in = null;//.InputStream class is the superclass of all classes representing an input
//stream of bytes.
   OutputStream out = null;//OutputStream class is the superclass of all classes r
//presenting an output stream of bytes.//for writing a file
//exception handling
   try{

   File file1 =new File("D:/story.txt");
   File file2=new File("D:/Copy_story.txt");

   in = new FileInputStream(file1);//This class is meant for reading streams
   out = new FileOutputStream(file2);//This class is meant for writing streams.

   byte[] buffer = new byte[1024];

   int len;
   //copy the file content in bytes
   while ((len = in.read(buffer)) > 0){

       out.write(buffer, 0, len);

   }

   in.close();
   out.close();

   System.out.println("File is copied successful!");

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

output

File is copied successful!