Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 3 File Output.

Similar presentations


Presentation on theme: "CHAPTER 3 File Output."— Presentation transcript:

1 CHAPTER 3 File Output

2 FILE NAMES Every input & output file actually 2 names.
 Real file name ⋄ used by operating system ⋄ use quotes when specifying inside program.  Name of the stream ⋄ connected to the file. ⋄ serves as a temporary name for file that is used within program.

3 TEXT FILE OUTPUT (PrintWriter)
import java.io.* Contains definitions for the class PrintWriter. CREATING / DEFINING AN OUTPUT TEXT FILE PrintWriter output_stream_name = null; PrintWriter class creates a file PrintWriter Preferred stream class for writing to a text file

4 OPENING THE OUTPUT TEXT FILE
output_stream_name = new PrintWriter (new FileOutputStream(“out_file_name”)); Connects output stream to file. Opens output text file. Program always starts with an empty file. If a file does not exist: New file is created Opening a file that already exists eliminates the old file and creates a new, empty one. Data in the original file is lost Connecting the stream named output_stream_name to the file named out_file_name This connecting is called opening the file. Program will always start with an empty file. PrintWriter Does not have a constructor that takes a file name as its arguments. Therefore, use FileOutputStream with the PrintWriter class. Exception thrown?? FileNotFoundException which should be caught in a catch block FileOutputStream constructor can throw this exception

5 … Text File Output … PrintWriter output_stream_variable = null;
output_stream_variable = new PrintWriter (new FileOutputStream (“file name”) ); Alternative: PrintWriter output_stream_variable = new PrintWriter (new FileOutputStream(“file name”) );

6 PrintWriter METHODS println - output_stream_variable.println(“………”);
Any combination, connected with + signs. The argument is output to the file connected to the stream. Ends the line and the next output is sent to the next line. print output_stream_variable.print(“………”); Does not end the line and the next output will be sent to the same line. close output_stream_variable.close(); Closes stream’s connection to a file. This method calls flush before closing the file. flush Flushes the output stream. Forces an actual physical write to the file of any data that has been buffered and not yet physically written to the file. Normally, you should not need to invoke flush.

7 Closing a file An Output file should be closed when you are done writing to it Use the close method. If a program ends normally it will close any files that are open

8 TextFileOutputDemo Part 1
public static void main(String[] args) { PrintWriter outputStream = null; outputStream = new PrintWriter( new FileOutputStream("out.txt")); Opening the file Creating a file can cause the FileNotFound-Exception if the new file cannot be made. try block: encloses only the opening of the file, because this is the only place an exception might be thrown. outputStream is declared outside the try block, so that it can be used outside the block. remember that anything declared inside a block (even a try block) is local to that block!!

9 TextFileOutputDemo Part 2
System.out.println("Enter three lines of text:"); String line = null; int count; for (count = 1; count <= 3; count++) { line = keyboard.readLine(); outputStream.println(count + " " + line); } outputStream.close(); System.out.println("... written to out.txt."); Writing to the file NB!!! Whenever the close method is invoked, the system releases any resources used to connect the stream to the file & does any other housekeeping that is needed. Closing the file The println method is used with two different streams: outputStream and System.out

10 Appending to a file PrintWriter Output_Stream_Name = new PrintWriter (new FileOutputStream(“file_name”, True_Boolean_Expression); If the file does not exist, Java will create an empty file of that name and append the output to the end of this empty file. May want to separate the declaration of the stream variable and the invocation of the constructor. After this, you can use the methods println and print to write to the file, and the new text will be written after the old text in the file.

11 CLASS EXERCISE Demonstrate processing until EOF
Write a program that copies all the lines from a file (story.txt) into another file (storylines.txt), one by one.

12 CLASS EXERCISE (SOLUTION)
import java.io.*; public class FileEx4 { public static void main (String[]args) String sentence = null; String inputFileName = "story.txt"; String outputFileName = "storylines.txt"; // declaring input file BufferedReader inFile = null; // declaring output file PrintWriter outFile = null;

13 //open input file inFile = new BufferedReader (new FileReader(inputFileName) ); // open output file outFile = new PrintWriter (new FileOutputStream(outputFileName) ); //priming read sentence = inFile.readLine(); while (sentence != null) { System.out.println("Copying " + sentence + “ to the new file."); outFile.println(sentence); sentence = inFile.readLine(); //read next } inFile.close(); outFile.close(); } //end main } //priming class


Download ppt "CHAPTER 3 File Output."

Similar presentations


Ads by Google