Write a program that will make a copy of a text file, line by line. Read the nam
ID: 3547687 • Letter: W
Question
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 isnt 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.txtExplanation / Answer
//very simple logic
//JUST ONE THING TRY TO KEEP THE INPUT FILE IN THE SAME LOCATION AS THE JAVA FILE OR
//UNDER THE PROJECT DIRECTORY IF WORKING ON NETBEANS
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
/**
*
* @author ganesh
*/
public class copyfile {
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
System.out.println("enter the name of existing file") ;
String existingfile=sc.nextLine();
File ff;
ff = new File(existingfile);
String address=ff.getAbsolutePath();
FileReader in=null;
BufferedReader br=null;
try
{
in=new FileReader(address);
}
catch(FileNotFoundException f)
{
System.out.println("file not found");
System.exit(1);
}
System.out.println("enter the name of new file"); //enter the name with extension
String newfile=sc.next();
File f=new File(newfile); //create the new file at the same place as existing file
int choice=0;
if(f.exists()) //check for existance of file
{
System.out.println("FILE ALREADY EXISTS 1 EXIT 2 OVERWRITE 3 ENTER A NEW NAME");
choice=sc.nextInt();
}
if(choice==1)
System.exit(1);
else if(choice==3)
{
System.out.println("enter a new name ");
newfile=sc.next();
}
String text=null;
try
{
br=new BufferedReader(in);
FileOutputStream o=new FileOutputStream(newfile); //creating a file outputstream overwriting if existing
while((text=br.readLine())!=null)
{
byte [] buf=text.getBytes();
System.out.println(text);
o.write(buf);
}
}
catch(Exception e)
{
System.out.println("Exception occured");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.