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.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lectures on File Management
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Text File I/O. Text 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.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Files and Streams CS 21a Chapter 11 of Horstmann.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Chapter 3b Standard Input and Output Sample Development.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Files Chap. 10 Streams, Readers, Writers 1. 2 Problem In our array example, we entered the students' names and scores from the keyboard. In many situations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 9 l Streams and Simple File I/O l Exception Handling with File.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Java Chapter 9 File Input and Output. Objectives In this chapter you will: Learn how bits, characters, records, and files fit into a data hierarchy Explore.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
 Pearson Education, Inc. All rights reserved Files and Streams.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
Files and console applications Chapter 17 This chapter explains: What a file is How to read and write to files How to manipulate folder paths and file.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
OO Design and Programming II I/O: Reading and Writing
Objectives You should be able to describe: Interactive Keyboard Input
University of Central Florida COP 3330 Object Oriented Programming
I/O Basics.
Chapter 13: File Input and Output
Streams and File I/O Chapter 14.
Topics Introduction to File Input and Output
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Topics Introduction to File Input and Output
Presentation transcript:

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 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 Files saved for word processors, spreadsheet programs, etc. Saved on secondary memory – 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 Problem: large amounts of weather-related data is recorded and processed – 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 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 BufferedReader in = new BufferedReader ( new FileReader (args[0])); Reading text from input file valueString = in.readLine(); Input file is a static object with readLine() method

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

Handling Exceptions Many things can go wrong when dealing with files – 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 A try block is followed by one or more "catch blocks" – Note the source code: catch (Exception anException) { 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 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 Executing Program Output Stream Input Stream

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

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

Using Readers and Writers Reader and Writer classes provide support for char (16-bit Unicode) I/O InputStream and OutputStream 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 Wrap FileReader in BufferedReader class BufferedReader inFile = new BufferedReader ( new FilReader (input_filename) ); Now the BufferedReader object ( inFile ) can be sent a.readLine() message A BufferedReader is so named because it buffers the input – this improves program performance

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

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

Using a Writer When writing values to a text file, author suggests use a Writer class – increase FileWriter capabilities by wrapping it in BufferedWriter and PrintWriter classes PrintWriter outFile = new PrintWriter( new BufferedWriter ( new FileWriter (file_name) ) );

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

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

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

Exceptions become more specialized Exception Hierarchy Most general class

Try/Catch Strategy Wrap all calls to methods that throw exceptions in single try block – 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 Method that throws an exception must explicitly state this in its heading public void read(BufferedReader aReader) throws EOFException Now calling method must invoke this method using try-catch blocks to handle the potential exception

10.4 More About I/O Streams Data can be stored in a file as text or in binary format – 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 – FileOutputStream – FileInputStream – Also BufferedOutputStream and BufferedInputStream

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

Writing Sequence of Values Consider a group of values of different types that are written to a file – 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 What is a virus? – software that hides itself within other executable programs – that executable program is the "host" for the virus – 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)read a line from a file b)if no line read due to EOF, terminate c)otherwise scan the line for specified string of text d)if that text found, stop reading from file 2.Display message whether or not target string was found – signifying virus found/not found Note source code, Figure 10.4

Simulated Virus Scanning Note use of command line arguments – 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 – studentID, a 9 digit integer – first name, last name as strings – studentYear as a string – credits and GPA as reals We seek a program which will – read a sequence of students from students.txt – prompt for studentID, read from keyboard – search for match, display if found

Objects TypeKindName Individual Student Student varying Number of students int varying numberOfStudents Sequence of students StudentSequence varying students Name of input file String constant args[0] A student ID number int varying studentID Position of the student int varying position

Student Class Operations needed include – initialize itself with default values – initialize itself with explicitly supplied values – read its attributes from a BufferedReader & 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 StudentSequence a)initialized from an input file b)name of file received in args[0] 2.Repeatedly do the following: a)prompt for and read studentID b)search students for target studentID, return its position c)if search successful display student at position otherwise display error message

Class StudentSequence Note source code, Figure 10.6 Reads numberOfStudents as first value in the file Constructor declares myStudents, an array of Student Use numberOfStudents as limiting value in for loop – stores incoming values in myStudents array Provides linear search method, find()

Student Information Retrieval Note source code, Figure 10.7 Program declares students, object of type StudentSequence – 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 Implement student data retrieval problem internet GUI context Much of work already done can be reused Behavior GUIStudentRetriever Enter Student ID : CreditsGPA BillBoard Freshman Alternatively, a "not found" message would appear here Bottom portion not visible until id entered and processed

Implementation Methods for GUIStudentRetreiver – 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 actionPerformed(ActionEvent event) { String idString = myIdField.getText(); if (idString.equals("")) enterInitialState(); else { int id = Integer.parseInt(idString); int position = myStudents.find(id); if (position >= 0) enterGoodIDState(myStudents.getStudent(position)); else enterErrorState("No student found with that ID"); } Note source code, Figure 10.8

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

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

Relational Model Database viewed as a set of tables, called relations – one row for each entry – column for each attribute or field of the database SQL, "Structured Query Language" – provides commands for manipulate data Example SELECT * FROM Employee WHERE Rate = 10.5 RetrieveAll records Name of Relation Condition