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: 3600075 • 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

CopyFile.java

import java.util.Scanner;

import java.io.FileWriter;

import java.io.File;

public class CopyFile {

public static void main(String[] args) {

String fileName;

Scanner console = new Scanner(System.in);

System.out.println("Enter name of the file to copy FileName --> ");

fileName=console.nextLine();

console.close();

try{

//input File

File file=new File(fileName);

//scanner for input file

Scanner fileScanner=new Scanner(file);

//writer for output file

FileWriter fw=new FileWriter("copyOf_" +fileName);

//copy

while(fileScanner.hasNext()){

fw.write(fileScanner.nextLine());

}

//close both files

fileScanner.close();

fw.close();

}catch(Exception e) {

System.out.println("Reading / Writing Failed") ;

}

}

}