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

JAVA CODING Write a program that will make a copy of a text file, line by line.

ID: 3773317 • Letter: J

Question

JAVA CODING

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. If the original file exists and there isn’t a file with the same name as the new file, copy the file. If a file with the name of the new file exists, 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. Create the existing file with a text editor (e.g. you may use Word but save the file as a text (.txt) file).

Hint: To find the text file, use a full path name when entering the file name, such as

C: extfilesStateCapitals.txt

You can save typing in the full path name by putting the file in the project directory, such as in

C:UsersmeDocumentsNetBeansProjectsProject4

and just typing in the file names.

Sample program executions follow:

Enter the name of the existing file: old.txt

Enter the name of the new file: new.txt

Copying old.txt into new.txt

Enter the name of the existing file: old.txt

Enter the name of the new file: new.txt

old.txt does not exist.

Exiting program.

Enter the name of the existing file: old.txt

Enter the name of the new file: new.txt

new.txt already exists

Choose from the following choices:

1 – Exit the program

2 – Overwrite the existing file

3 – Enter a new name for the file

Enter a number: 1

Exiting program

Enter the name of the existing file: old.txt

Enter the name of the new file: new.txt

new.txt already exists

Choose from the following choices:

1 – Exit the program

2 – Overwrite the existing file

3 – Enter a new name for the file

Enter a number: 2

Overwriting new.txt

Enter the name of the existing file: old.txt

Enter the name of the new file: new.txt

new.txt already exists

Choose from the following choices:

1 – Exit the program

2 – Overwrite the existing file

3 – Enter a new name for the file

Enter a number: 3

Enter a new name for the file: newer.txt

Copying old.txt into newer.txt

JAVA CODING

Explanation / Answer

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileCopyMod {

   public static void main(String[] args)throws IOException
   {
      
       InputStreamReader ir =new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(ir);
      
       System.out.println("Enter the name of the existing file");
       String oldFileName=br.readLine();
      
       System.out.println("Enter the name of the new file");
       String newFileName=br.readLine();
      
       File dir = new File(".");
         
       String source = dir.getCanonicalPath() + File.separator + oldFileName;
       String dest = dir.getCanonicalPath() + File.separator + newFileName;
      
       File oldFile=new File(source);

       if(oldFile.exists() && oldFile.canRead())
       {
           File newFile=new File(dest);
           if(newFile.exists())
           {
               System.out.println(newFileName+" already exists");
               System.out.println("Choose from the following choices:");
               System.out.println("1 – Exit the program");
               System.out.println("2 – Overwrite the existing file");
               System.out.println("3 – Enter a new name for the file");
               System.out.print("Enter a number: ");
               int option=Integer.parseInt(br.readLine());
              
               if(option==1)
               {
                   System.out.println("Exiting program.");
                   System.exit(0);
               }
               else if(option==2)
               {
                   System.out.println("Overwriting "+newFileName);
                   copy(oldFile,dest);
               }
               else if(option==3)
               {
                   System.out.print("Enter a new name for the file:");
                   newFileName=br.readLine();
                  
                   System.out.println("Copying "+oldFileName+" into "+newFileName);
                   copy(oldFile,dest);
               }
           }
           else
           {
               System.out.println("Copying "+oldFileName+" into "+newFileName);
               copy(oldFile,dest);
            }
       }
       else
       {
           System.out.println(oldFileName+".txt does not exist");
System.out.println("Exiting program.");
System.exit(0);
       }
      
       System.exit(0);
   }
  
   public static void copy(File oldFile,String dest)
   {
       FileInputStream fis = null;
       try
       {
           fis = new FileInputStream(oldFile);
       }
       catch (FileNotFoundException e)
       {
           e.printStackTrace();
       }
      
       BufferedReader in = new BufferedReader(new InputStreamReader(fis));

       FileWriter fstream = null;
       try
       {
           fstream = new FileWriter(dest, true);
           BufferedWriter out = new BufferedWriter(fstream);

           String aLine = null;
           while ((aLine = in.readLine()) != null)
           {
               out.write(aLine);
               out.newLine();
           }
           in.close();
           out.close();
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
   }

}