Download LECTURE_11 - Illinois Institute of Technology

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 116
OBJECT ORIENTED PROGRAMMING II
LECTURE 11
GEORGE KOUTSOGIANNAKIS
Copyright: 2016 / Illinois Institute of Technology/George Koutsogiannakis
1
Last Week’ s Topics
• Exception Handling
– Using try and catch Blocks
– Catching Multiple Exceptions
– User-Defined Exceptions
2
New Topics
• The java.io Package
• Reading Text Files
• Reading Structured Text Files Using
StringTokenizer
• Reading using Streams
• Writing to Structured Text Files
3
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).
4
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.
5
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.
6
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 .
7
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.
8
Selected Input Classes in
the java.io Package
Class
InputStream
FileInputStream
Description
Abstract superclass representing a
stream of raw bytes
Input stream to read raw bytes of data
from files
ObjectInputStream Class to read/recover objects from a
file written using ObjectOutputStream
9
Hierarchy for Input Classes
10
Selected java.io Output Classes
Class
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
11
Hierarchy for Output Classes
12
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()); }
}
}
13
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.
15
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.
16
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.
17
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
18
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.
19
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).
20
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.
21
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
22
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.
23
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(); }
24
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(myarray, 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).
25
Example Reading Text File
• The char array myarray, needs to be instantiated to the correct size before
it is used in a while loop to capture the characters read. Therefore we
need to loop twice through the file.
– The first time to capture the count of characters (as in slide 24). We use the value of
count to set the size of the char array. Close the stream objects used!!!
– The second time we create a new set of stream objects and we loop again through the
file , this time using the read method that take s the char array as argument as in slide
25
• Keep in mind that an array of char types (myarray) can be converted to a
String by using:
String str=new String(myarray);
We can output the String by using:
System.out.println(str);
26
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
27
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.
28
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.
void
newLine( )
writes a line separator. Throws an
IOException.
void
close( )
releases resources allocated to the
BufferedWriter object. Throws an IOException.
See Example 11.9 WriteTextFile.java
and Example 11.10 AppendTextFile.java
29
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
30
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
31
Useful PrintWriter Methods
Return value
void
Method name and argument list
print( dataType argument )
writes a String representation of the argument
to an output stream
void
println( dataType argument )
writes a String representation of the argument
to an output stream followed by a newline.
void
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
32
Example: Writing to a Text File
Suppose we want to write a String aString to a File. Notice that the file name in the
FileOutputStream constructor is a String type.
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!
33
Study Guide
• Chapter 11
Sections: 11.2, 11.3, 11.4, 11.5, 11.6
34