Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - File Rename

flamenca91
flamenca91 


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:\textfiles\StateCapitals.txt You can save typing in the full path name by putting the file in the project directory, such as in C:\Users\me\Documents\NetBeansProjects\Project4 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 Attachments
Tags Clarifications

Answers

Java - File Rename - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - File Rename assignment including source and comments Attachments
FileRename.java (2 K) Download Preview
of the new file: "); String newFileName = input.nextLine(); //check if old file exists File oldFile = new File(oldFileName); if(!oldFile.exists()){ System.out.println(oldFileName + " does not exist"); System.out.println("Exiting program"); return; } //check if new file exists File newFile =

Screenshots
Purchase Answer

Solution Programming Class 4.txt

mariobeka
mariobeka 



Attached you'll find the solution for the assignment named CopyFile.java which includes the code and comments. Also screenshots of the working program are included. Attachments
CopyFile.java (3 K) Download Preview
java.io.InputStreamReader; import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.StandardCopyOption; public class CopyFile { public static void main(String[] args) throws InterruptedException, IOException { File source; // source file File dest; // destination file // Copy Options CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES }; // Buffer reader to get inputs from the user BufferedRe

old.txt (1 K) Download
Screen Shot 2014-04-29 at 1.19.06 PM.png (24 K) Download Preview

Screen Shot 2014-04-29 at 1.19.27 PM.png (33 K) Download Preview

Screen Shot 2014-04-29 at 1.20.27 PM.png (34 K) Download Preview

Screen Shot 2014-04-29 at 1.21.03 PM.png (24 K) Download Preview

Screenshots
Screenshot
Screenshot
Screenshot
Screenshot
Purchase Answer