1 / 11

CHAPTER 3

CHAPTER 3. File Input. 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.

holland
Download Presentation

CHAPTER 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 3 File Input

  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. Inputting (Reading) Data from a File • Use class FileReader • Parameter: file name & location • read • Only reads character by character • Use class BufferedReader • Parameter: FileReader object • readln • Reads entire line of characters

  4. EXAMPLE: FileReader inData = new (FileReader(“a:\\prog.dat”); BufferedReader inFile = new BufferedReader(inData); OR BufferedReader inFile = new BufferedReader(newFileReader("a:\\prog.dat"));

  5. TEXT FILE INPUT (BufferedReader) • TEXT FILE INPUT: import java.io.* Contains definitions for the class BufferedReader. • DEFINING AN INPUT TEXT FILE BufferedReader input_stream_name = null; input_stream_name = new BufferedReader (new FileReader (“file name”) ); Alternative: BufferedReader input_stream_name = new BufferedReader (new FileReader (“file name”) );

  6. Text File Input • Can be created via Notepad or a program. • i.e. “file name” • OPENING THE INPUT TEXT FILE BufferedReader input_stream_name = new BufferedReader (new FileReader(“file_name”)); • EXCEPTIONS FileNotFoundException (opening the file) IOException (reading the file) • Should be thrown in method header or caught in a catch-block.

  7. BufferedReader METHODS • readln Reads a line of input (String). If the read goes beyond the end of the file, then null is returned. E.g. input_stream_name.readLine(); • read Reads a single character (integer) & returns that character as an int value. If the read goes beyond the end of the file, then -1 is returned. Because it returns an int, to obtain a char, you must perform a type cast. char next = (char)(inputStream.read()); • close Closes stream’s connection to a file.

  8. Example:Reading a File Name from the Keyboard reading a file name from the keyboard using the file name read from the keyboard closing the file reading data from the file 8

  9. Common methods to test for the End of an input file (EOF) 1. Put a sentinel value at the end of the file and test for it. 2. Test for a special character that signals the end of the file (text files often have such a character). Special character • readLine returns null • read returns -1 (the int value of all ordinary characters is nonnegative)

  10. Example: Using Null toTest for End-of-File in a Text File When using readLine test for null Excerpt from TextEOFDemo When using read test for -1 10

  11. CLASS EXERCISE • Demonstrate processing until EOF Write a program that processes an input file (story.txt). It must display the number of vowels in each record. The program must also display final vowel totals after all the lines (i.e. records) have been processed.

More Related