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

Read the linked Java™ code carefully. Then, answer the following questions in a

ID: 3915278 • Letter: R

Question

Read the linked Java™ code carefully.

Then, answer the following questions in a Microsoft® Word file:

1. As you run the program in NetBeans the first time, at the prompt (the program will pause for input) type abc Return def Return ghi Ctrl+Shift+Del. What is the result?

2. As you run the program in NetBeans the second time, at the prompt (the program will pause for input) type 123 Ctrl+Shift +Del. What is the result?

3. What happens if the file Data.txt already exists when you run the program?

4. What happens if the file Data.txt does not already exist when you run the program?

/**********************************************************************
*   Program:   FileOut  
*    Purpose:    Demonstrate the coding to produce output to a file.   
*   Programmer:   I am student          
*   Class:       PRG/421r13, Java Programming II          
*   Instructor:              
*   Creation Date:   01/03/2018  
*
***********************************************************************/
package fileout;
import java.io.*;


public class FileOut {

public static void main(String[] args) {

InputStream istream;
OutputStream ostream=null;
   // FileOutputStream creates an OutputStream that you can use to write bytes to a file.
int c;                     // character stream  
final int EOF = -1;                 // EOF indicator
istream = System.in;

  
// If the Data.txt file already exists, present its contents on the console.
String fileName = "Data.txt";         // name of the file to open.
String line = null;               // will reference one line at a time

try {
FileReader fileReader =        // FileReader reads text file
new FileReader(fileName);         // reads in data from the file

// always wrap FileReader in BufferedReader (to verify)
BufferedReader bufferedReader =
new BufferedReader(fileReader);

System.out.println("Here are the contents of the current file named " + fileName + ": ");
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);        // verify / display what is read in by the program
}   
bufferedReader.close();         // close file

}
catch(FileNotFoundException ex) {       // coding to verify file can be opened
System.out.println(           // if not open, error message to display
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {           // exception, when there is an error in reading
System.out.println(
"Error reading file '"
+ fileName + "'");

}
  

// Now, let's construct a new file containing user input.
System.out.println(" Type characters to write to file. After you finish, press Ctrl+Shift+Del to end.");
File outFile = new File("Data.txt");    // create a new file

try {                     // try block for EOF indicator
ostream = new FileOutputStream(outFile);
while ((c = istream.read()) != EOF)     // look for end of file in istream
ostream.write(c);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
try {                     // try block for file error ñ file did not close

istream.close();             // close input and output
ostream.close();
} catch (IOException e) {
System.out.println("File did not close");
}
}
}
}

Explanation / Answer

Please find your answers below. Thanks

1. As you run the program in NetBeans the first time, at the prompt (the program will pause for input) type abc Return def Return ghi Ctrl+Shift+Del. What is the result?

When the program run for the first time (no Data.txt exist initially), and we typed ‘abc’ ->enter -> ‘def’ -> enter ->’ghi ‘ -> not pressing enter key, but Ctrl+Shift+Delete, the Data.txt file has been created with following data

abc

def

Note that ‘ghi’ is not appearing because we didn’t press enter key after typing it. The file write happens as long as we do not press the EOF key code which is Ctrl+ Shift+ Delete

2. As you run the program in NetBeans the second time, at the prompt (the program will pause for input) type 123 Ctrl+Shift +Del. What is the result?

When we run the program for the second time, the existing data on ‘Data.txt’ will be displayed first. Then if we type 123 and pressed enter key before inputting Ctrl+Shift +Del, then the contents of Data.txt will be overwritten by ‘123’. If we didn’t press enter key, then 123 will not be written, but the data file is overwritten with empty data

3. What happens if the file Data.txt already exists when you run the program?

If the Data.txt already exists, then its contents are displayed initially, and then the file is overwritten with the data we enter. Even if we do not enter any data, the Data.txt file will be overwritten with a blank document

4. What happens if the file Data.txt does not already exist when you run the program?

If Data.txt does not exist when we run the program, then the system will print a message ‘Unable to open file 'Data.txt' ‘ and then create a new file Data.txt