I need help with this java program Thanks Write a program th

I need help with this java program. Thanks

Write a program that will make a copy of a text file, line by line. Read the name of the existing file and the name of the new file-the copy-from the keyboard. Use the methods of the class File to test whether the original file exists and can be read. If not, display an error message and abort the program. Similarly, see whether the name of the new file already exists. If so, display a warning message and allow the user to either abort the program, overwrite the existing file, or enter a new name for the file.

Solution

FileCopy.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class FileCopy {

   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the input file name: \");
       String input = scan.next();
       File file = new File(input);
       if (file.exists()) {
           System.out.print(\"Enter the output file name: \");
           String output = scan.next();
           File outputfile = new File(output);
           while (outputfile.exists()) {
               System.out
                       .println(\"Output file already exist. Do you want to override it (y or n): \");
               char ch = scan.next().charAt(0);
               if (ch == \'n\' || ch == \'N\') {
                   System.out.print(\"Enter the output file name: \");
                   output = scan.next();
                   outputfile = new File(input);
               } else {
                   break;
               }

           }
           Scanner scan1= new Scanner(file);
           PrintWriter pw = new PrintWriter(outputfile);
           while(scan1.hasNextLine()){
               pw.println(scan1.nextLine());
           }
           pw.flush();;
           pw.close();
           scan1.close();
           scan.close();
           System.out.println(\"File has been copied from \"+input+\" to \"+output);
          
       } else {
           System.out.println(\"File does nto exist\");
       }

   }

}

Output:

Enter the input file name: D:\\\\mary.txt
Enter the output file name: D:\\\\mary_output.txt
Output file already exist. Do you want to override it (y or n):
y
File has been copied from D:\\\\mary.txt to D:\\\\mary_output.txt

I need help with this java program. Thanks Write a program that will make a copy of a text file, line by line. Read the name of the existing file and the name o
I need help with this java program. Thanks Write a program that will make a copy of a text file, line by line. Read the name of the existing file and the name o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site