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

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"); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!