Java I/O Java IO You will be reading from and writing to files. You should promp
ID: 3707078 • Letter: J
Question
Java I/O
Java IO
You will be reading from and writing to files. You should prompt the user for a manifest file name, which should contain the name of additional files, one per line. Then, you should read the contents of each of these files and write them to a single new file. The name of the new file should be the name of the manifest with the additional extension .out. If there is a problem reading or writing to a file, you should print an appropriate error message. If you try to read a file from your project, it will look for it in the application root. This is inside the same folder as src. Place new files there (Right-Click on project in workspace, then File > New File to create an empty, extension-less file).
Input files
manifest.txt
file1.txt Hello
file2.txt World
file3.txt How are you?
Output
Enter manifest filename: manifest.txt
manifest.txt.out
Hello
World
How are you?
package lab10;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
Code Template:
public class L10 {
public static void main(String[] args) {
System.out.println("input manifest file name");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
// TODO
// create a reader and writer object
// use the reader to read the names of input files from the manifest file and
// for each file name, use the same reader to read the content of the file, and write to the output file.
// don't forget to flush the writer in the end
in.close();
}
}
Explanation / Answer
L10.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class L10 {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("input manifest file name");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
// TODO
// create a reader and writer object
// use the reader to read the names of input files from the manifest file and
// for each file name, use the same reader to read the content of the file, and write to the output file.
// don't forget to flush the writer in the end
File file = new File(name);
if(file.exists()) {
Scanner scan = new Scanner(file);
File outputFile = new File(name+".out");
PrintWriter pw = new PrintWriter(outputFile);
while(scan.hasNextLine()) {
String fileName = scan.nextLine();
File inFile = new File(fileName);
Scanner read = new Scanner(inFile);
while(read.hasNextLine()) {
pw.write(read.nextLine()+" ");
}
read.close();
}
pw.flush();
pw.close();
scan.close();
System.out.println("File has been generated");
} else {
System.out.println("File does not exist");
}
in.close();
}
}
Output:
input manifest file name
D://manifest.txt
File has been generated
manifest.txt.out
Hello
World
How are you?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.