Respuesta :
Answer:
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
Answer:
/*
** Lab 11: Write Program that will make a copy of text file
line by line. Use these steps.
** Author: .......
** Date: 04/24/2022
*/
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
class lab11 {
public static void main(String[] args)
{
try {
// Read Existing file name
File readInputFile = new File("textInput.txt");
// Read Content of the Existing file
String content = Files.readString(Paths.get("textInput.txt"));
// Check if Existing file can read or exist
File inputExits = new File("textInput.txt");
// check if expecting new file exist
File outputExists = new File("textOutput.txt");
// nested scanner in try block
try (Scanner keyboard = new Scanner(System.in)) {
// create space for console
System.out.println("");
// validate if the exisitng file already there
if(inputExits.exists() && !inputExits.isDirectory() && inputExits.canRead()) {
System.out.println("Original File Read...");
}else{
// throw error because existing file is not there
throw new IOException("File does not exists!");
}
// read the existings file name
System.out.println("Existing file name: " + readInputFile.toString());
// check if the expecting new file already exist
if(!outputExists.exists() && !outputExists.isDirectory()){
// if not already exist create new one right away
FileWriter output = new FileWriter("textOutput.txt");
// write all the content from existing file to the new expecting file
for (int i = 0; i < content.length(); i++){
output.write(content.charAt(i));
}
System.out.print("Create new file now: textOutput.txt");
// close the filewriter
output.close();
}else if(outputExists.exists()) {
// expecting file name already exists
System.out.println("");
System.out.println("This new file name is already exists!");
System.out.println("Press 1 to end the program");
System.out.println("Press 2 to overwrite the existing file");
System.out.println("Press 3 to enter the new name for the file");
// get the conditions from keyboard
int condition = keyboard.nextInt();
if(condition == 1){
// end program right away for condition from keyboard === 1
System.out.println("End Program!");
}
if(condition == 2 ){
// overwrite by recreate the filewriter
FileWriter overwriteWithNewText = new FileWriter("textOutput.txt", false);
// write the content from existing file to the new file
for (int i = 0; i < content.length(); i++){
overwriteWithNewText.write(content.charAt(i));
}
overwriteWithNewText.close();
}
if(condition == 3){
// create the new file with the new name from console
String newName = keyboard.next();
FileWriter newFileName = new FileWriter(newName+".txt");
// write the exisitng content to the new file
for (int i = 0; i < content.length(); i++){
newFileName.write(content.charAt(i));
}
newFileName.close();
}
}
}
}
catch (IOException e) {
// catch error if all above had problems
System.out.println(
e);
}
}
}
Explanation:
Hi, here is the sample code, give it a tweak and change because there was repetitive stuff I create for example the FileWriter for each condition if the new file name already exists. Try to create a global variable and use it through multiple places, but here since I am a newbie, I would prefer this logic to be able to see how my code work line by line.
Not Complete Code for the Project, But to give you a general Idea on How to Start