Welcome to AssignmentCache!

PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

Availability: In stock

$6.00

Quick Overview

PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File


"Demonstrate the Coding to Produce Output to a File" text file
For this assignment, you will analyze Java™ that presents instructional text on the console, accepts user input, and then creates a file based on that user input.
Read the linked Java™ code carefully.
Then, answer the following questions in a Microsoft® Word file:
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?
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?
What happens if the file Data.txt already exists when you run the program?
What happens if the file Data.txt does not already exist when you run the program?


Submit your Word file to the Assignment Files tab.


/**********************************************************************
*   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 + ":\n");
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("\nType 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");
}
}
}
}

PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

Double click on above image to view full picture

Zoom Out
Zoom In

More Views

  • PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File
$6.00

Details

PRG 421 Week 2 Individual Analyze Assignment Demonstrate the Coding to Produce Output to a File

"Demonstrate the Coding to Produce Output to a File" text file
For this assignment, you will analyze Java™ that presents instructional text on the console, accepts user input, and then creates a file based on that user input.
Read the linked Java™ code carefully.
Then, answer the following questions in a Microsoft® Word file:
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?
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?
What happens if the file Data.txt already exists when you run the program?
What happens if the file Data.txt does not already exist when you run the program?

Submit your Word file to the Assignment Files tab.

/**********************************************************************
*   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 + ":\n");
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("\nType 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");
}
}
}
}

Additional Information

Special Price $4.00

Product Tags

Use spaces to separate tags. Use single quotes (') for phrases.

[profiler]
Memory usage: real: 18350080, emalloc: 17994208
Code ProfilerTimeCntEmallocRealMem