Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Problem Statement In this lab section, you are going to learn how to read da

ID: 3729533 • Letter: T

Question

The Problem Statement

In this lab section, you are going to learn how 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 <enter>

At the prompt type: the second line <enter>

At the prompt type: this line contains 8, 9, 10 <enter>

At the prompt type: stop <enter>

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.

According to the information given above, draw a class diagram in the space below (using UML notation).

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote