// File: io/readwrite/CopyTextFile.java // Description: Example to show text fil
ID: 3628892 • Letter: #
Question
// File: io/readwrite/CopyTextFile.java// Description: Example to show text file reading and writing.
// Author: Fred Swartz
// Date : February 2006
import java.io.*;
import java.util.*;
public class CopyTextFile {
public static void main(String args[]) {
//... Get two file names from use.
System.out.println("Enter a filepath to copy from, and one to copy to.");
Scanner in = new Scanner(System.in);
//... Create File objects.
File inFile = new File(in.next()); // File to read from.
File outFile = new File(in.next()); // File to write to
//... Enclose in try..catch because of possible io exceptions.
try {
copyFile(inFile, outFile);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
}
//=============================================================== copyFile
// Uses BufferedReader for file input.
public static void copyFile(File fromFile, File toFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));
//... Loop as long as there are input lines.
String line = null;
while ((line=reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // Write system dependent end of line.
}
//... Close reader and writer.
reader.close(); // Close to unlock.
writer.close(); // Close to unlock and flush to disk.
}
//=============================================================== copyFile2
// Uses Scanner for file input.
public static void copyFile2(File fromFile, File toFile) throws IOException {
Scanner freader = new Scanner(fromFile);
BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));
//... Loop as long as there are input lines.
String line = null;
while (freader.hasNextLine()) {
line = freader.nextLine();
writer.write(line);
writer.newLine(); // Write system dependent end of line.
}
//... Close reader and writer.
freader.close(); // Close to unlock.
writer.close(); // Close to unlock and flush to disk.
}
}
Please identify the problem
Explanation / Answer
Please find my answer in red.. // File: io/readwrite/CopyTextFile.java // Description: Example to show text file reading and writing. // Author: Fred Swartz // Date : February 2006 import java.io.*; import java.util.*; public class CopyTextFile { public static void main(String args[]) { //... Get two file names from use. int i=10; System.out.println("Enter a filepath to copy from, and one to copy to."); System.out.println("Debug point..."); Scanner in = new Scanner(System.in); //... Create File objects. File inFile = new File(in.next()); // File to read from. File outFile = new File(in.next()); // File to write to //... En close in try..catch because of possible io exceptions. try { i=i+1; copyFile(inFile, outFile); } catch (IOException e) { System.err.println(e); System.exit(1); } } //=============================================================== copyFile // Uses BufferedReader for file input. public static void copyFile(File fromFile, File toFile) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(fromFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(toFile)); //... Loop as long as there are input lines. String line = null; while ((line=reader.readLine()) != null) { writer.write(line); writer.newLine(); // Write system dependent end of line. } //... Close reader and writer. reader.close(); // Close to unlock. writer.close(); // Close to unlock and flush to disk. } //=============================================================== copyFile2 // Uses Scanner for file input. public static void copyFile2(File fromFile, File toFile) throws IOException { Scanner freader = new Scanner(fromFile); BufferedWriter writer = new BufferedWriter(new FileWriter(toFile)); //... Loop as long as there are input lines. String line = null; while (freader.hasNextLine()) { line = freader.nextLine(); writer.write(line); writer.newLine(); // Write system dependent end of line. } //... Close reader and writer. freader.close(); // Close to unlock. writer.close(); // Close to unlock and flush to disk. } } Please identify the in red
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.