Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS"— Presentation transcript:

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

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

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

4 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).

5 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.

6 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.

7 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 .

8 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.

9 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

10 Hierarchy for Input Classes

11 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

12 Hierarchy for Output Classes

13 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()); }

14 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.

15 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.

16 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.

17 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.

18 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

19 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.

20 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).

21 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.

22 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 FlightRecord.java and Example ReadFlights.java

23 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.

24 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(); }

25 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).

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

27 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

28 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.

29 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 AppendTextFile.java

30 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

31 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

32 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 WriteGradeFile.java

33 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!

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


Download ppt "OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS"

Similar presentations


Ads by Google