OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 15: I/O and Parsing
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
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.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS 1 Copyright: FALL 2014 Illinois Institute of Technology_ George Koutsogiannakis.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and Input/Output Operations
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.
CSC – Java Programming II Lecture 9 January 30, 2002.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
Lecture 5 I/O and Parsing
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
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.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
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.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
OO Design and Programming II I/O: Reading and Writing
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Lesson 8: More File I/O February 5, 2008
Object Writing in files
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
I/O Basics.
Programming in Java Files and I/O Streams
CS 116 Object Oriented Programming II
Streams and File I/O Chapter 14.
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
CSS 161: Fundamentals of Computing
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Lecture 18 File I/O CSE /5/2019.
David Davenport Spring 2005
Presentation transcript:

OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS CS 201 OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis

Last Week’ s Topics Exception Handling Using try and catch Blocks Catching Multiple Exceptions User-Defined Exceptions

New Topics The java.io Package Reading Text Files Reading Structured Text Files Using StringTokenizer Reading using Streams Writing to Structured Text Files

File Types Java basically supports two types of files: text files: data is stored as characters binary files: data is stored as raw bytes The type of a file is determined by the classes used to write to the file. Types of Files can be: Files written using Bytes Streams (1s and 0s). Files written using Character Streams (char types). Files written in terms of Strings (text). Files written using primitive data types (int, double etc) Files written using entire object streams (recording all information about an object in a coded format).

File Types To read an existing file, you must know the file's type in order to select and import the appropriate library classes for reading the file.

Writing to Text Files Several situations can exist: the file does not exist the file exists and we want to replace the current contents the file exists and we want to append to the current contents We specify whether we want to replace the contents or append to the current contents.

Streams File writing and reading is accomplished with streams. There are input and output streams The term input means input to your program or consul from a file. The term output means from your program or consul to a file .

Streams Therefore: To read a file input stream is needed. To write to a file output stream is needed. The java.io package provides the classes for establishing input and output streams.

Selected Input Classes in the java.io Package Description InputStream Abstract superclass representing a stream of raw bytes FileInputStream Input stream to read raw bytes of data from files ObjectInputStream Class to read/recover objects from a file written using ObjectOutputStream

Hierarchy for Input Classes

Selected java.io Output Classes Description Writer Abstract superclass for output classes OutputStreamWriter Class to write output data streams OutputStream Abstract superclass representing an output stream of raw bytes FileWriter Class for writing to character files BufferedWriter More efficient writing to character files PrintWriter Prints basic data types, Strings, and objects PrintStream Supports printing various data types conveniently FileOutputStream Output stream for writing raw bytes of data to files ObjectOutputStream Class to write objects to a file

Hierarchy for Output Classes

Reading Text Files with Scanner In CS115 we used the File object and the Scanner object to read a text file i.e. import java.io.File; import java.io.IOException; import java.util.Scanner; Public class ReadFile { Public static void main(String[] args) { try { File myfile=new File(“mytextfiel.txt”); Scanner scan=new Scanner(myfile); while(scan.hasMoreTokens()) String str=scan.next(); System.out.println(str); } catch)(IOException ioe) { System.out.println(ioe.toString()); }

Opening and Closing an Input Stream When we construct an input stream or output stream object, the JVM associates the file name, standard input stream, or standard output stream with our object. This is opening the file. When we are finished with a file, we optionally call the close method to release the resources associated with the file. Return value Method name and argument list void close( ) releases resources associated with an open input stream. Throws an IOException.

Closing an output stream When closing a Buffered output stream in addition to the close() method for the stream the methdod flush() should also be invoked: datastreamobject.flush(); Flushing the output stream forces any buffered output bytes to be written out.

Opening and Closing Standard Streams Some streams are standard and the are available to any java program. The standard input stream (System.in), the standard output stream (System.out), and the standard error stream (System.err) are open when the program begins. They are intended to stay open and should not be closed.

SOFTWARE ENGINEERING TIP Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released. It is good practice to call the close method, however, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standard input, output, or error devices, however. They are intended to remain open.

Exceptions While Reading from a File We can catch this exception: FileNotFoundException thrown by the Scanner constructor if the filename is not found when opening the file We do not expect these exceptions to occur, so we will catch them as subclasses of IOException, and print the stack trace. InputMismatchException if the input does not match the expected data type. (The next method does not throw this exception, so we don’t need to catch this exception). NoSuchElementException if we attempt to read beyond the end of the file. IllegalStateException if we attempt to read after calling the close method. See Example 11.8 ReadTextFile.java

Reading Structured Text Files Some text files are organized into lines that represent a record -- a set of data values containing information about an item. The data values are separated by one or more delimiters; that is, a special character or characters that separate one value from the next. As we read the file, we need to parse each line; that is, separate the line into the individual data values called tokens.

Example An airline company could store data in a file where each line represents a flight segment containing the following data: flight number origin airport destination airport number of passengers average ticket price  These are called headers or field names of the record. Such a file could contain the following data: AA123,BWI,SFO,235,239.5 AA200,BOS,JFK,150,89.3 … In this case, the delimiter is a comma. This example shows two records (or two rows of data).

Remember the StringTokenizer Class? The StringTokenizer class is designed to parse Strings into tokens. StringTokenizer is in the java.util package. When we construct a StringTokenizer object, we specify the delimiters that separate the data we want to tokenize. The default delimiters are the whitespace characters.

Example Using StringTokenizer The file flight.txt contains the following comma-separated flight data on each line: flight number, origin airport, destination airport, number of passengers, average ticket price The FlightRecord class defines instance variables for each flight data value. The ReadFlights class reads data from flights.txt, instantiates FlightRecord objects, and adds them to an ArrayList. See Examples 11.12 FlightRecord.java and Example 11.13 ReadFlights.java

Reading using Streams (without the Scanner object) Suggestion: from now on import the entire java.io package (java.io.*;) Text files: files created by using Strings based streams ( written using the class PrintWriter). Use: class PrintReader to read it. Character Files: files created using char types (written using FileWriter class). Use: class FileReader to read it. Bytes Files -binary data. (written using class FileOutputStream) Use: class FileInputStream to read it. Primitive Data Types Files: files that have recorded strictly primitive data types (written using various methods depending on the primitive data type of class DataOutputStream ) Use: class DataInputStream methods to read file. These classes offer different write methods for primitive data types i.e. public final void writeDouble(double v) throws IOException Files that have Objects recorded: files written using ObjectOutputStream class methods. Use: class ObjectInput methods to read the objects from the file.

Example: Reading Text File try{ FileInputStream file=new FileInputStream("MyFile.txt"); BufferedReader filereader=new BufferedReader(new InputStreamReader(file)); int index=0; int count=0; while(index>=0) { count++; index=filereader.read(); System.out.println(count); } file.close(); filereader.close(); }// end of try catch(IOException ioe){ System.out.println(ioe.toString(); }

Example Reading Text File In this example a buffer stream was used. The buffer stream wraps the File Stream. The while loop and when we encounter -1, since this is the indicator that the end of a file has been reached (-1 is added to the end of the file when we create the file). Calling the read method of InputStreamReader class returns an int .If the end of the file is reached the value of the int is -1. The method reads one character at a time. In this example we are not saving what we read. The count counts the number of readings made in order to possibly create an array. We can repeat the loop once we have created an array and this time save the readings into the array by using a different format of the read method: int index=filereader.read(array, 0, count-1); this call places the readings into the array, where array is an array of char data types (from 0th index to the last index).

Example Reading Text File An array of char types can be converted to a String by using: String str=new String(array);

Constructors for Writing to Text Files Class Constructor FileWriter FileWriter( String filename, boolean mode ) constructs a FileWriter object from a String representing the name of a file. If the file does not exist, it is created. If mode is false, the current contents of the file, if any, will be replaced. If mode is true, writing will append data to the end of the file. Throws an IOException. BufferedWriter BufferedWriter( Writer w ) constructs a BufferedWriter object from a Writer object

Buffering Writing data to the disk is much slower than writing data into main memory. When a class uses buffering, instead of writing one character at a time to the file, it accumulates the data in a temporary location in memory, called a buffer. When the buffer is full or is flushed, the accumulated data is written to the file in one efficient operation.

Methods of the BufferedWriter Class Return value Method name and argument list void write( String s ) writes a String to the current OutputStream object. This method is inherited from the Writer class. Throws an IOException. newLine( ) writes a line separator. Throws an IOException. close( ) releases resources allocated to the BufferedWriter object. Throws an IOException. See Example 11.9 WriteTextFile.java and Example 11.10 AppendTextFile.java

Writing Primitive Types to Text Files FileOutputStream, a subclass of the OutputStream class, is designed to write a stream of bytes to a file. The PrintWriter class is designed for converting primitive data types to characters and writing them to an output stream. print method, writes data without a newline println method, writes data then adds a newline

Constructors for Writing to Structured Text Files Class Constructor FileOutputStream FileOutputStream( String filename, boolean mode ) constructs a FileOutputStream object from a String representing the name of a file. If the file does not exist, it is created. If mode is false, the current contents of the file, if any, will be replaced. If mode is true, writing will append data to the end of the file. Throws a FileNotFoundException. PrintWriter PrintWriter( OutputStream os ) constructs a PrintWriter object from an OutputStream object

Useful PrintWriter Methods Return value Method name and argument list void print( dataType argument ) writes a String representation of the argument to an output stream println( dataType argument ) writes a String representation of the argument to an output stream followed by a newline. close( ) releases the resources associated with the PrintWriter object The argument can be any primitive data type (except byte or short), a char array, or an object. See Example 11.15 WriteGradeFile.java

Example: Writing to a Text File Suppose we want to write a String aString to a File try { FileOutputStream file = new FileOutputStream(filename, true); OutputStreamWriter filestream = new OutputStreamWriter(new BufferedOutputStream(file)); filestream.write(aString); filestream.flush(); file.close(); } Catch(IOException ioe) { System.out.ptintln(ioe.toString()); If more than one String is to be written then a loop can be used. Notice that the flush method needs to be called at the end to flush out the stream into the file. If this is not done the String is not written in the file!

Study Guide Chapter 11 Sections: 11.2, 11.3, 11.4, 11.5, 11.6