Unformatted text preview:

Chapter 10StreamsSlide 3Text Files and Binary FilesSlide 5Writing to a Text FileSlide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13File NamesA File Has Two NamesIOExceptionUnchecked ExceptionsPitfall: a try Block is a BlockAppending to a Text FiletoString Helps with Text File OutputSome Methods of the Class PrintWriter (Part 1 of 3)Some Methods of the Class PrintWriter (Part 2 of 3)Some Methods of the Class PrintWriter (Part 3 of 3)Reading From a Text File Using ScannerReading Input from a Text File Using Scanner (Part 1 of 4)Reading Input from a Text File Using Scanner (Part 2 of 4)Reading Input from a Text File Using Scanner (Part 3 of 4)Reading Input from a Text File Using Scanner (Part 4 of 4)Testing for the End of a Text File with ScannerChecking for the End of a Text File with hasNextLine (Part 1 of 4)Checking for the End of a Text File with hasNextLine (Part 2 of 4)Checking for the End of a Text File with hasNextLine (Part 3 of 4)Checking for the End of a Text File with hasNextLine (Part 4 of 4)Checking for the End of a Text File with hasNextInt (Part 1 of 2)Checking for the End of a Text File with hasNextInt (Part 2 of 2)Methods in the Class Scanner (Part 1 of 11)Methods in the Class Scanner (Part 2 of 11)Methods in the Class Scanner (Part 3 of 11)Methods in the Class Scanner (Part 4 of 11)Methods in the Class Scanner (Part 5 of 11)Methods in the Class Scanner (Part 6 of 11)Methods in the Class Scanner (Part 7 of 11)Methods in the Class Scanner (Part 8 of 11)Methods in the Class Scanner (Part 9 of 11)Methods in the Class Scanner (Part 10 of 11)Methods in the Class Scanner (Part 11 of 11)Reading From a Text File Using BufferedReaderSlide 48Reading From a Text FileReading Input from a Text File Using BufferedReader (Part 1 of 3)Reading Input from a Text File Using BufferedReader (Part 2 of 3)Reading Input from a Text File Using BufferedReader (Part 3 of 3)Slide 53Some Methods of the Class BufferedReader (Part 1 of 2)Some Methods of the Class BufferedReader (Part 2 of 2)Reading NumbersTesting for the End of a Text FilePath NamesSlide 59Slide 60Slide 61Slide 62Nested Constructor InvocationsSlide 64System.in, System.out, and System.errSlide 66Slide 67Slide 68The File ClassSome Methods in the Class File (Part 1 of 5)Some Methods in the Class File (Part 2 of 5)Some Methods in the Class File (Part 3 of 5)Some Methods in the Class File (Part 4 of 5)Some Methods in the Class File (Part 5 of 5)Class FileSlides prepared by Rose Williams, Binghamton University Chapter 10File I/O© 2006 Pearson Addison-Wesley. All rights reserved 10-2Streams•A stream is an object that enables the flow of data between a program and some I/O device or file–If the data flows into a program, then the stream is called an input stream–If the data flows out of a program, then the stream is called an output stream© 2006 Pearson Addison-Wesley. All rights reserved 10-3Streams•Input streams can flow from the keyboard or from a file–System.in is an input stream that connects to the keyboard Scanner keyboard = new Scanner(System.in);•Output streams can flow to a screen or to a file–System.out is an output stream that connects to the screenSystem.out.println("Output stream");© 2006 Pearson Addison-Wesley. All rights reserved 10-4Text Files and Binary Files•Files that are designed to be read by human beings, and that can be read or written with an editor are called text files–Text files can also be called ASCII files because the data they contain uses an ASCII encoding scheme–An advantage of text files is that they are usually the same on all computers, so that they can move from one computer to another© 2006 Pearson Addison-Wesley. All rights reserved 10-5Text Files and Binary Files•Files that are designed to be read by programs and that consist of a sequence of binary digits are called binary files–Binary files are designed to be read on the same type of computer and with the same programming language as the computer that created the file–An advantage of binary files is that they are more efficient to process than text files–Unlike most binary files, Java binary files have the advantage of being platform independent also© 2006 Pearson Addison-Wesley. All rights reserved 10-6Writing to a Text File•The class PrintWriter is a stream class that can be used to write to a text file–An object of the class PrintWriter has the methods print and println–These are similar to the System.out methods of the same names, but are used for text file output, not screen output© 2006 Pearson Addison-Wesley. All rights reserved 10-7Writing to a Text File•All the file I/O classes that follow are in the package java.io, so a program that uses PrintWriter will start with a set of import statements:import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;•The class PrintWriter has no constructor that takes a file name as its argument–It uses another class, FileOutputStream, to convert a file name to an object that can be used as the argument to its (the PrintWriter) constructor© 2006 Pearson Addison-Wesley. All rights reserved 10-8Writing to a Text File•A stream of the class PrintWriter is created and connected to a text file for writing as follows:PrintWriter outputStreamName;outputStreamName = new PrintWriter(new FileOutputStream(FileName));–The class FileOutputStream takes a string representing the file name as its argument–The class PrintWriter takes the anonymous FileOutputStream object as its argument© 2006 Pearson Addison-Wesley. All rights reserved 10-9Writing to a Text File•This produces an object of the class PrintWriter that is connected to the file FileName–The process of connecting a stream to a file is called opening the file–If the file already exists, then doing this causes the old contents to be lost–If the file does not exist, then a new, empty file named FileName is created•After doing this, the methods print and println can be used to write to the file© 2006 Pearson Addison-Wesley. All rights reserved 10-10Writing to a Text File•When a text file is opened in this way, a FileNotFoundException can be thrown–In this context it actually means that the file could not be created–This type of exception can also be thrown when a program attempts to open a file for reading and there is no such file•It is therefore necessary to enclose this code in exception handling blocks–The file should be


View Full Document

SJU CSC 3405 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?