110 likes | 197 Views
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.
E N D
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. ⋄ serves as a temporary name for file that is used within program.
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
EXAMPLE: FileReader inData = new (FileReader(“a:\\prog.dat”); BufferedReader inFile = new BufferedReader(inData); OR BufferedReader inFile = new BufferedReader(newFileReader("a:\\prog.dat"));
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”) );
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.
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.
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
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)
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
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.