Java Chapter 9 File Input and Output Objectives
Java Chapter 9 File Input and Output
Objectives In this chapter you will: n Learn how bits, characters, records, and files fit into a data hierarchy n Explore the differences between text files and binary files n Use the File class to identify, manage, and manipulate files n Perform data input and output with text files n Perform data input and output with binary files
File Input and Output Most real-world applications use data files n Data stored in files are called persistent data n Reading data from a file is called file input n Writing data to a file is called file output n File processing refers to file input or file output n
File Input and Output (continued) n Two types of files ¨ Text files contain only human-readable characters ¨ Binary files contain human-readable characters and other characters understood by the software or hardware n Files containing data needed by an application may be either text or binary
Inputting Data from a Text File Many programs are designed to use external data n External data can be numeric n ¨ Stock n prices, GPS coordinates temperatures External data can be string ¨ Names, n descriptions, passwords If such data is stored permanently in a data file, and is designed to be humanreadable, it is a text file
Text File Organization Recall data ultimately is a sequence of 0 s and 1 s, which represent two states of electronic circuits: on and off n A localized group of 8 bits forms a byte and bytes represent characters n Example: ‘A’ is 65 in Unicode, 01000001 in binary: n 0 X 27+ 1 X 26+ 0 X 25+ 0 X 24+ 0 X 23+ 0 X 22+ 0 X 21+ 1 X 20
Text File Organization (continued) n A data field is a group of characters that has a specific meaning ¨ Example: n last_name, student_ID, test_score A data record is a group of related fields ¨ Example: Smith 12345 95. 5 ¨ Attributes of the same individual n A data file is a group of related data records stored in a single file
Text File Organization (continued)
The File Class n n n The File class establishes the file’s name and location and opens the file for input If the file is in a directory other than the current, its path must be specified The relative path is the path of folders that leads to the file relative to the current file The absolute path is the path from the drive letter to the file The syntax for declaring a File object: File my. Fl = new File (“. /mydata. txt”);
The File. Reader and Buffered. Reader Classes n n n After the data file is established using a File object, the data in the file can be read A source file provides data to a program Analogous to a pipeline ¨ The pipeline has 2 ends connected to the source (the input file) and destination (the program file) ¨ The pipeline has a valve that controls the amount of data allowed into the program
The File. Reader and Buffered. Reader Classes (continued)
The File. Reader and Buffered. Reader Classes (continued) n Establish a data source File infl = new File (“. /source. txt”); n Create a pipeline from source to program File. Reader frdr = new File. Reader (infl); n Create a valve Buffered. Reader a. Bfrd = new Buffered. Reader(frdr);
The File. Reader and Buffered. Reader Classes (continued) n Read one line of data String a. Rcrd = a. Bfrd. read. Line(); n Input can also be read using the Scanner class File infl = new File (. /source. txt); Scanner input = new Scanner (infl); String a. Fld = input. next();
Apply the Concept n n n Develop an application to read IDs, names, and scores of students from a file and display the average score for each student A while loop tests whether the program is at the end of the student names file As IDs are read scores are read and total. Score and score. Count are updated
Working with Directories The file object has many methods. n The file can be a folder or directory. n Review the API documentation for all of the methods. n ¨ http: //java. sun. com/j 2 se/1. 4. 2/docs/api/index. html n Note the list() and list(filter) methods that can be used to return a list of files (as well as other folders) in a folder.
Outputting Data to a Text File Previously we have accessed data from a source file n Next, we output data to a destination file n A destination file receives data from a program n
The File Class File objects for output are created just as for input n Unlike source files, destination files do not have to exist before the program is run n If the destination file already exists, it is overwritten when the program is run n If the destination file does not exist, it is created n
The File Class (continued)
The File. Writer and Print. Writer Classes n n n Instantiate a File object to create the data destination Instantiate a File. Writer object to create a pipeline from the program to the output file File. Writer throws an IOException if it can’t create the output file Instantiate a Print. Writer object to enable writing to the output file Use the println method in Print. Writer to write the output
The File. Writer and Print. Writer Classes (continued)
Apply the Concept n n n Modify previous example to print each student’s score average to file, as well as to the command window The application uses the same input files Import java. io. IOException, java. io. File. Writer, and java. io. Print. Writer Instantiate a File object for writing output Print. Writer is declared outside of the try block to be accessible in the finally block
Apply the Concept (continued) n A runtime error can occur if the output file location is nonexistent
Apply the Concept (continued)
Performing Input and Output with Binary Files n n The previous sections discussed reading from and writing to text files Java classes Scanner, File. Reader, Buffered. Reader, File. Writer, and Print. Writer work with pipelines or streams that carry text data All files not classified as text files are binary files Binary files can be compiled programs, image files, sound files, compressed files
Identifying an Input/Output File The process of using the File class is the same for text files and binary files n Create a binary file to write to in the current directory File file 1 = new File ( “. /my. File. dat” ); n
Writing to a Binary File n To write to a text file there are 5 steps: File o. Fl = new File ( “averages. txt”); File. Writer fwt = new File. Writer (o. Fl); Print. Writer pwt = new Print. Writer (fwt); pwt. println (a. Line. Of. Data); pwt. close(); n To write to a binary file, substitute: for File. Writer ¨ Data. Output. Stream for Print. Writer ¨ write. Char for println ¨ File. Output. Stream
Reading from a Binary File To read from a binary file, identify the input file using a File object n A File. Input. Stream object connects the input file to the program n The Data. Input. Stream allows different types of data to be read n Methods in Data. Input. Stream read different types of data n
Reading from a Binary File
Summary n n n Data is arranged in a hierarchy: files, records, fields, characters, bits The File class identifies a file to the program so that it can be read from or written to An absolute path is the path from the drive letter to the file A relative path is the path to the file relative to the current file The class File. Reader reads a continuous stream of characters from a text file
Summary (continued) n n n The class Buffered. Reader controls the flow of characters through the File. Reader object The class File. Writer establishes a data stream from the program to a text file The class Print. Writer enables writing formatted text to a text file The classes File. Output. Stream and Data. Output. Stream write program data to binary files The classes File. Input. Stream and Data. Input. Stream read data from binary files
- Slides: 30