File IO Chapter 10 Chapter Contents Chapter Objectives

File I/O Chapter 10

Chapter Contents Chapter Objectives 10. 1 Introductory Example: Weather Data Analysis 10. 2 Java/s I/O System: Readers, Writers, Streams 10. 3 Exceptions 10. 4 More About I/O Streams 10. 5 Example: Scanning for a Virus 10. 6 Example: Student Data Retrieval 10. 7 Graphical/Internet Java: Student Data Retrieval GUI Part of the Picture: Data Base Systems

Chapter Objectives l l Study Java's I/O with text and binary files Examine role of exceptions in I/O Observe use of files in GUI programs in context of an information retrieval problem Learn about the role of files in database management systems

Need for File Access l l Files saved for word processors, spreadsheet programs, etc. Saved on secondary memory – l hard disks, floppy disks, CD-ROMs, etc. Requirements information stored in secondary memory – – can be retrieved in the future kept separate from other documents, programs, etc.

Intro Example: Weather Data Analysis l Problem: large amounts of weather-related data is recorded and processed – – – l l year's worth of atmospheric pressure readings taken every 15 minutes stored in a text file pressure. dat Weatherman needs minimum, maximum, and average of the data Data must be read from file, statistics calculated, results written to a text output file

Solution Overview l l l Note source code, Figure 10. 1 Names of input and output files are given to the program via the args parameter – input file is args[0] – output file is args[1] Command to open the input file Buffered. Reader in = new Buffered. Reader ( new File. Reader (args[0])); l Reading text from input file value. String = in. read. Line(); Input file is a static object with read. Line() method

Solution Overview l Opening text output file Print. Writer out = new Print. Writer ( new Buffered. Writer( new Filewriter (args[1]))); l Writing text values to a file out. println (. . . ); l Closing the files in. close(); out. close();

Handling Exceptions l Many things can go wrong when dealing with files – – – l requested input file does not exist input file has invalid data output file unavailable When such an error occurs, the method in which the problem happened "throws an exception" – – Java can "catch" the exception if it happens in a "try block" Note the try { … } block in the source code

Handling Exceptions l A try block is followed by one or more "catch blocks" – l Note the source code: catch (Exception an. Exception) { Controller. fatal ( … ) ; } This catch block will – – – determine the kind of exception report the problem terminate the program

10. 2 Java's I/O System: Readers, Writers, and Streams l l l All input and output in Java is accomplished by classes called streams Input streams provide ways to move bytes of data from an input device to a program Output streams move the data in the opposite direction Output Stream Input Stream Executing Program

Predefined Streams l System. in – l System. out – l Input. Stream object, usually for the keyboard a buffered Print. Stream object, usually the screen or an active window System. err – an unbuffered Print. Stream object usually associated with the screen or console window

Wrapping Classes l Input. Stream class provides methods for reading bytes only – l to read at a higher level we must "wrap" System. in with another class Example: Buffered. Reader class Buffered. Reader my. Reader = new (Buffered. Reader ( new Input. Stream. Reader( System. in)); l Now we can send my. Reader either – – the. read() message for a single char value or … the. read. Line() message for an entire line of text

Using Readers and Writers l l l Reader and Writer classes provide support for char (16 -bit Unicode) I/O Input. Stream and Output. Stream provide support for byte I/O The Java language is still growing and "maturing" – – extensions of Reader and Writer classes have yet to be fully accepted author of this text suggests: Use Reader or Writer class whenever possible, use Stream class only when there is no Reader/Writer class that fits

Using a Reader l Wrap File. Reader in Buffered. Reader class Buffered. Reader in. File = new Buffered. Reader ( new Fil. Reader (input_filename) ); l l Now the Buffered. Reader object (in. File) can be sent a. read. Line() message A Buffered. Reader is so named because it buffers the input – this improves program performance

Using a Reader l How to know when all the data in a file has been read? – the read. Line() method returns the value null value. String = in. File. read. Line(): while (value. String != null) { … // process value. String = in. File. read. Line(); l } Also possible to check for other things such as an empty line with no text if (value. String. equals("")) continue;

Using a Reader Sequence of steps for using text input 1. Build a Buffered. Reader by wrapping one around a File. Reader 2. Use an input loop to read lines of text from the Buffered. Reader l Convert those lines to numeric values if necessary 3. Close the Buffered. Reader

Using a Writer l When writing values to a text file, author suggests use a Writer class – increase File. Writer capabilities by wrapping it in Buffered. Writer and Print. Writer classes Print. Writer out. File = new Print. Writer( new Buffered. Writer ( new File. Writer (file_name) ) );

10. 3 Exceptions l Easy for things to go wrong in a program – – l faulty logic bad input from user, etc. Java classifies all these as "exceptional occurrences" – provides Exception class – tells something abnormal occurred

Try-Catch Blocks l To signal an abnormal occurrence – – method can throw an exception throw occurs inside a try block followed by one or more catch blocks Exception thrown here Form: try { … statement list … } Program)searches catch (Exception_Type 1 variable_name 1 catch blocks looking { … } for type match for catch (Exception_type 2 variable_name 2) thrown exception When match found, that block of { … } statements executed finally { … } l

Try-Catch Blocks l After catch block executed – – l If no matching exception type is found – l execution continues in finally { } block or … if no finally { }, continues to next statements following catch { } program terminates If no exception thrown – – catch blocks skipped execution continues in finally { } or in statements following catch block

Exception Hierarchy Most general class Exceptions become more specialized

Try/Catch Strategy l Wrap all calls to methods that throw exceptions in single try block – – l followed by single catch block. . or … followed by sequence of catch blocks, one for each type of exception Wrap each call to a method that throws an exception in its own try block – a catch block exists for each particular exception thrown

Throwing Exceptions l Method that throws an exception must explicitly state this in its heading public void read(Buffered. Reader a. Reader) throws EOFException l Now calling method must invoke this method using try-catch blocks to handle the potential exception

10. 4 More About I/O Streams l Data can be stored in a file as text or in binary format – – l same format used to store values in memory takes less space to store an integer in binary format than in text format Java Stream classes provide byte level I/O – File. Output. Stream – File. Input. Stream – Also Buffered. Output. Stream and Buffered. Input. Stream

Writing Primitive types l l Java provides Data. Output. Stream and Data. Input. Stream classes Note methods provided, Table 10. 1 – l note number of bytes used to store the data Consider Data Stream Demonstration, Figure 10. 3 in text – note the features Why does…contents of – – numbers. dat wrapping of Stream I/O classes use of try-catch blocks appear to be gibberish? specific methods used to write, then read sample run

Writing Sequence of Values l Consider a group of values of different types that are written to a file – l they must be read in the same order that they were written Possible to create a class – – – that group of values is the private data of the class specific methods are provided for an object of the class to write/read those values to/from a file the read and write methods receive the file handles as parameters

10. 5 Example: Scanning for a Virus l What is a virus? – – l software that hides itself within other executable programs that executable program is the "host" for the virus program tries to "infect" other programs the virus can cause problems that range from annoying to malicious A virus that prints an annoying message stores that string value within itself – we will write a program that searches through a file to find that text, or signature

Virus-Detection Algorithm 1. Repeat the following: a) b) c) d) read a line from a file if no line read due to EOF, terminate otherwise scan the line for specified string of text if that text found, stop reading from file 2. Display message whether or not target string was found – signifying virus found/not found l Note source code, Figure 10. 4

Simulated Virus Scanning l Note use of command line arguments – – l first argument is the name of the file to be read second argument is the target string or virus signature Observe other features studied in this chapter – – use of try/catch wrapping of classes for file read

10. 6 Example: Retrieving Student Information Problem: Information Retrieval Records of information about students consist of l l – student. ID, a 9 digit integer – – first name, last name as strings student. Year as a string – credits and GPA as reals We seek a program which will – read a sequence of students from students. txt prompt for student. ID, read from keyboard – search for match, display if found –

Objects Type Kind Name Individual Student varying Number of students int varying number. Of. Students Sequence of students Student. Sequence varying students Name of input file String constant args[0] A student ID number int varying student. ID Position of the student int varying position

Student Class l Operations needed include – – – l initialize itself with default values initialize itself with explicitly supplied values read its attributes from a Buffered. Reader & store them in itself convert itself to a String (for output) access each of its attributes mutate any of its attributes Note methods provided for these capabilities in source code of Figure 10. 5

Program Algorithm 1. Build students as a Student. Sequence a) initialized from an input file b) name of file received in args[0] 2. Repeatedly do the following: a) prompt for and read student. ID b) search students for target student. ID, return its position c) if search successful display student at position otherwise display error message

Class Student. Sequence l l Note source code, Figure 10. 6 Reads number. Of. Students as first value in the file Constructor declares my. Students, an array of Student Use number. Of. Students as limiting value in for loop – l stores incoming values in my. Students array Provides linear search method, find()

Student Information Retrieval l l Note source code, Figure 10. 7 Program declares students, object of type Student. Sequence – l constructor loads values from file Forever loop used to – – – prompt for, receive as input student id call. find method to determine position of that student in the array print results (found or not)

10. 7 Graphical/Internet Java: Student Data Retrieval GUI l l l Implement student data retrieval problem internet GUI context Much of work already done can be reused Behavior GUIStudent. Retriever Bottom portion not visible until id entered and processed Alternatively, a "not found" message would appear here Enter Student ID : Bill Freshman 111223333 Board Credits 16. 0 GPA 3. 15

Implementation l Methods for GUIStudent. Retreiver – – – l constructor action handler enter initial state enter found an ID state enter not found state Attributes – – – labels entry fields panes for panels

Event-Handler Method public void action. Performed(Action. Event event) { String id. String = my. Id. Field. get. Text(); if (id. String. equals("")) enter. Initial. State(); else { int id = Integer. parse. Int(id. String); int position = my. Students. find(id); if (position >= 0) enter. Good. IDState(my. Students. get. Student(position)); else enter. Error. State("No student found with that ID"); } } Note source code, Figure 10. 8

Part of the Picture: Database Systems l Businesses must make decisions l Based on data – – l accuracy and detail help make better decisions large data sets stored on computers Data sets maintained by Database Management Systems (DBMS)

DBMS Facilities l l High level views of the data Access routines – l Support for large databases – l data is sensitive or proprietary Data sharing – l as large as 1000 gigabytes (one terabyte) Security – l query language protect data when more than one person wishes to access/ modify same piece of data Data integrity

Relational Model l Database viewed as a set of tables, called relations – – l SQL, "Structured Query Language" – l one row for each entry column for each attribute or field of the database provides commands for manipulate data Example SELECT * FROM Employee WHERE Rate = 10. 5 Retrieve All records Name of Relation Condition
- Slides: 41