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: 3773316 • 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 Copy the source code and the output into a word processing document.

JAVA CODING

Explanation / Answer

import java.io.*;
import java.util.Scanner ;
public class Project4
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintWriter outputStream = null;
Scanner inputStream = null;
System.out.println("Please enter the name of the existing file: ");
String fileName = scan.nextLine();
System.out.println("Please enter the name of the new file: ");
String fileName2 = scan.nextLine();
File fileObject = new File(fileName) ;
File fileObject2 = new File(fileName2);
if(fileObject.exists())
{
if(fileObject2.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.println("Enter a number: ");
int choice = scan.nextInt();
if(choice == 1)
{
System.out.println("Exiting program");
System.exit(0);
}
if(choice == 2)
{
System.out.println("Overwriting " + fileName2);
try
{
inputStream = new Scanner(new File(fileName));
outputStream = new PrintWriter(fileName2);
while(inputStream.hasNextLine())
{
String input = inputStream.nextLine();
outputStream.print(input);
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println("Error");
}
}
if(choice == 3)
{
System.out.println("Enter a new name for the new file: ");
String newName = scan.nextLine();
try
{
inputStream = new Scanner(new File(fileName));
outputStream = new PrintWriter(newName);
System.out.println("Copying " + fileName + " into " + newName);
while(inputStream.hasNext())
{
String input = inputStream.nextLine();
outputStream.println(input);
}
inputStream.close();
outputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error");
System.exit(0);
}
}
}
else
{
try
{
inputStream = new Scanner(new File(fileName));
outputStream = new PrintWriter(fileName2);
while(inputStream.hasNextLine())
{
String input = inputStream.nextLine();
outputStream.println(input);
System.out.println("Copying " + fileName + " into " + fileName2);
}
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
else
{
System.out.println(fileName + " does not exist.");
System.exit(0);
}
}
}