The Problem Statement In this lab section, you are going to read data from keybo
ID: 3729498 • Letter: T
Question
The Problem Statement
In this lab section, you are going to read data from keyboard and write the data to a disk file. You are going to write an application that has one class (simple enough). One requirement for this lab is to use the following classes: BufferedReader, InputStreamReader, System.in, FileWriter (cannot use Scanner class).
This application will display the following message in the DOS console:
“Enter a line of text (‘stop’ to quit), then hit enter.)
: “
prompting the user type a line of string. The program will keep offer column symbol “: “ until the user typed ‘stop’. For each line typed, the program will write it to a disk file named “myData.txt” (make sure that each new line is stored as separate line). The disk should be automatically closed when the program exits.
Please follow the following sequence of steps to generate the content (Test Case 1).
Test Case 1
At the prompt type: the first line
At the prompt type: the second line
At the prompt type: this line contains 8, 9, 10
At the prompt type: stop
Go to the appropriate directory, find and open the myData.txt file.
The Directory that the FileWriter will Write File to
Sometime, a programmer (especially the ones new to Java) will wonder where the input file should be put or where the written file is created. In this lab, we used one file written method
FileWriter fw = new FileWriter("myData.txt").
You will be given the opportunity of finding out where the file will be located.
SCORE YOUR POINTS BY DOING THE FOLLOWING
According to the information given above, draw a class diagram in the space below (using UML notation) (4 pts).
Answer:
Programming work
After debugging the code, cut and paste the Java source code in the space below (4 pts).
Answer:
The input file and output file are usually located at the root of a project. In the space below, give the absolute directory path for your project’s “bin” and the absolute directory path of the output file “myData.txt” (including the file) (2 pts).
Answer:
Output the file content (in the space below, cut and paste the content of the output file myData.txt) (5 pts).
Answer:
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class WriteToFile {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String line = "";
FileWriter fw = new FileWriter("myData.txt");
System.out.print("Enter a line of text (‘stop’ to quit): ");
line = br.readLine();
while(!line.equalsIgnoreCase("stop")) {
fw.write(line);
fw.write(" ");
System.out.print("Enter a line of text (‘stop’ to quit): ");
line = br.readLine();
}
fw.close();
br.close();
}
}
/*
Sampe run:
Enter a line of text (‘stop’ to quit): this is the first line
Enter a line of text (‘stop’ to quit): this is the second line
Enter a line of text (‘stop’ to quit): stop
*/
#### myData.txt ######
this is the first line
this is the second line
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.