Programming Methodology: Comp 249
Programming Methodology: Comp 249
Programming Methodology
Chapter 10 – File I/O
Prof. Aiman Hanna
Department of Computer Science & Software Engineering
Concordia University, Montreal, Canada
These slides have been extracted, modified and updated from original slides of Absolute Java 3rd Edition by
Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is
published by Pearson Education / Addison-Wesley.
10-2
Streams
Input streams can flow from the keyboard or from a
file
System.in is an input stream that connects to the
keyboard
Scanner kb = new Scanner(System.in);
10-3
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 text
files
Text files can also be called ASCII (American Standard Code
for Information Interchange) files because the data they
contain uses an ASCII encoding scheme
10-4
Text Files and Binary Files
Files that are designed to be read by programs and that
consist of a sequence of binary digits are called binary
files
Binary files are designed to be read on the same type of
computer and with the same programming language as the
computer that created the file
Unlike most binary files, Java binary files have the advantage
of being platform independent also
10-5
Writing to a Text File
The class PrintWriter is a stream class that
can be used to write to a text file
An object of the class PrintWriter has the
methods print and println
10-6
Writing to a Text File
All the file I/O classes that follow are in the package java.io
10-7
Writing to a Text File
A stream of the class PrintWriter is created and
connected to a text file for writing as follows:
PrintWriter pw;
pw = new PrintWriter(new
FileOutputStream(FileName));
The class FileOutputStream takes a string representing the file
name as its argument
10-8
Writing to a Text File
This produces an object of the class PrintWriter
that is connected to the file FileName
The process of connecting a stream to a file is called opening
the file
If the file already exists, then doing this causes the old
contents to be lost
If the file does not exist, then a new, empty file named
FileName is created
10-10
Writing to a Text File
When a program is finished writing to a file, it should
always close the stream connected to that file; i.e.
pw.close();
If the program does not close the file before the program
ends, Java will close it automatically, but data loss may occur
10-11
Writing to a Text File
Output streams connected to files are usually buffered
10-12
Writing to a Text File
The method close invokes the method flush, thus
insuring that all the data is written to the file
The sooner a file is closed after writing to it, the less likely it
is that there will be a problem
10-13
File Names
The rules for how file names should be formed
depend on a given operating system, not Java
When a file name is given to a Java constructor for a
stream, it is just a string, not a Java identifier (e.g.,
"fileName.txt")
10-14
A File Has Two Names
Every input file and every output file used by a
program has two names:
1. The real file name used by the operating system
2. The name of the stream that is connected to the file
See FileIO1.java
10-15
IOException
When performing file I/O there are many situations in which an
exception, such as FileNotFoundException, may be
thrown
10-16
Pitfall: a try Block is a Block
Since opening a file can result in an exception, it should be
placed inside a try block
10-17
Appending to a Text File
To create a PrintWriter object and connect it
to a text file for appending, a second argument, set
to true, must be used in the constructor for the
FileOutputStream object
outputStreamName = new PrintWriter(new
FileOutputStream(FileName, true));
The new text will be written after the old text in the file
See FileIO3.java
10-18
toString Helps with Text File Output
10-19
Reading From a Text File Using Scanner
The class Scanner can be used for reading from the keyboard
as well as reading from a text file
Simply replace the argument System.in (to the Scanner
constructor) with a suitable stream that is connected to the text file
Scanner sc =
new Scanner(new FileInputStream(FileName));
See FileIO4.java
10-20
Testing for the End of a Text File with
Scanner
A program that tries to read beyond the end of a
file using methods of the Scanner class will cause
an exception to be thrown
However, instead of having to rely on an exception
to signal the end of a file, the Scanner class
provides methods such as hasNextInt and
hasNextLine
These methods can also be used to check that the next
token to be input is a suitable element of the appropriate
type
See FileIO5.java
See FileIO6.java
10-21
Using both Input & Output Files
Both input and output files are often used within
the same program
See FileIO7.java
See FileIO8.java
10-22
Methods in the Class Scanner
(Part 1 of 11)
10-23
Methods in the Class Scanner
(Part 2 of 11)
10-24
Methods in the Class Scanner
(Part 3 of 11)
10-25
Methods in the Class Scanner
(Part 4 of 11)
10-26
Methods in the Class Scanner
(Part 5 of 11)
10-27
Methods in the Class Scanner
(Part 6 of 11)
10-28
Methods in the Class Scanner
(Part 7 of 11)
10-29
Methods in the Class Scanner
(Part 8 of 11)
10-30
Methods in the Class Scanner
(Part 9 of 11)
10-31
Methods in the Class Scanner
(Part 10 of 11)
10-32
Methods in the Class Scanner
(Part 11 of 11)
10-33
Reading From a Text File Using
BufferedReader
The class BufferedReader is a stream class that can be used
to read from a text file
An object of the class BufferedReader has the methods read and
readLine
10-34
Reading From a Text File Using
BufferedReader
Like the classes PrintWriter and Scanner,
BufferedReader has no constructor that takes a file
name as its argument
It needs to use another class, FileReader, to convert the file
name to an object that can be used as an argument to its (the
BufferedReader) constructor
10-35
Reading From a Text File
After these statements, the methods read and
readLIne can be used to read from the file
The readLine method is the same method used to read
from the keyboard, but in this case it would read from a file
Since the read method does not return the character itself, a
type cast must be used:
char next = (char)(readerObject.read());
10-36
Reading From a Text File
A program using a BufferedReader object in
this way may throw two kinds of exceptions
An attempt to open the file may throw a
FileNotFoundException (which in this case has
the expected meaning)
An invocation of readLine may throw an
IOException
Both of these exceptions should be handled
See FileIO9.java
10-37
Testing for the End of a Text File
The method readLine of the class
BufferedReader returns null when it tries to
read beyond the end of a text file
A program can test for the end of the file by testing for the
value null when using readLine
10-38
Some Methods of the Class
BufferedReader (Part 1 of 2)
10-39
Some Methods of the Class
BufferedReader (Part 2 of 2)
10-40
Reading Numbers
Unlike the Scanner class, the class BufferedReader
has no methods to read a number from a text file
Instead, a number must be read in as a string, and then converted
to a value of the appropriate numeric type using one of the
wrapper classes
10-41
Path Names
When a file name is used as an argument to a
constructor for opening a file, it is assumed that
the file is in the same directory or folder as the
one in which the program is run
10-42
Path Names
A path name not only gives the name of the file,
but also the directory or folder in which the file
exists
10-44
Path Names
The Windows operating system specifies path names in a
different way
An example of Windows path name is as follows:
C:\Concordia\Comp249\Assg2\data.txt
A BufferedReader input stream connected to this file is created
as follows:
BufferedReader inputStream = new
BufferedReader(new FileReader
("C:\\Concordia\\Comp249\\Assg2\\data.txt"));
10-45
Path Names
A double backslash (\\) must be used for a Windows path name enclosed
in a quoted string
This problem does not occur with path names read in from the
keyboard
10-46
Nested Constructor Invocations
Each of the Java I/O library classes serves only
one function, or a small number of functions
Normally two or more class constructors are
combined to obtain full functionality
10-47
Nested Constructor Invocations
new BufferedReader(new FileReader("stuff.txt"))
Above, the anonymous FileReader object establishes a
connection with the stuff.txt file
However, it provides only very primitive methods for input
10-48
System.in, System.out, and
System.err
The standard streams System.in, System.out, and
System.err are automatically available to every Java program
System.out is used for normal screen output
System.err is used to output error messages to the screen
10-49
System.in, System.out, and
System.err
Using these methods, any of the three standard streams
can be redirected
For example, instead of appearing on the screen, error
messages could be redirected to a file
10-50
System.in, System.out, and
System.err
Redirecting System.err:
public void getInput()
{
. . .
PrintStream errStream = null;
try
{
errStream = new PrintStream(new
FileOuptputStream("errMessages.txt"));
System.setErr(errStream);
. . . //Set up input stream
}
10-51
System.in, System.out, and
System.err
catch(FileNotFoundException e)
{
System.err.println("Input file not found");
}
finally
{
. . .
errStream.close();
}
}
10-52
The File Class
The File class is like a wrapper class for file names
The constructor for the class File takes a name, (known as
the abstract name) as a string argument, and produces an object
that represents the file with that name
10-53
Some Methods in the Class File
(Part 1 of 5)
10-54
Some Methods in the Class File
(Part 2 of 5)
10-55
Some Methods in the Class File
(Part 3 of 5)
10-56
Some Methods in the Class File
(Part 4 of 5)
10-57
Some Methods in the Class File
(Part 5 of 5)
See FileIO10.java
See FileIO11.java
10-58
Binary Files
Binary files store data in the same format used by
computer memory to store the values of variables
No conversion needs to be performed when a value is stored
or retrieved from a binary file
10-59
Writing Simple Data to a Binary
File
The class ObjectOutputStream is a stream class that can
be used to write to a binary file
An object of this class has methods to write strings, values of primitive
types, and objects to a binary file
10-60
Opening a Binary File for Output
An ObjectOutputStream object is created and
connected to a binary file as follows:
ObjectOutputStream outputStreamName = new
ObjectOutputStream(new
FileOutputStream(FileName));
10-61
Opening a Binary File for Output
After opening the file, ObjectOutputStream methods can
be used to write to the file
Methods used to output primitive values include writeInt,
writeDouble, writeChar, and writeBoolean
10-63
Some Methods in the Class
ObjectOutputStream (Part 2 of 5)
10-64
Some Methods in the Class
ObjectOutputStream (Part 3 of 5)
10-65
Some Methods in the Class
ObjectOutputStream (Part 4 of 5)
10-66
Some Methods in the Class
ObjectOutputStream (Part 5 of 5)
10-67
Reading Simple Data from a Binary File
10-68
Opening a Binary File for
Reading
An ObjectInputStream object is created and
connected to a binary file as follows:
ObjectInputStream inStreamName = new
ObjectInputStream(new
FileInputStream(FileName));
10-69
Opening a Binary File for
Reading
After opening the file, ObjectInputStream methods can
be used to read to the file
Methods used to input primitive values include readInt,
readDouble, readChar, and readBoolean
If the file contains multiple types, each item type must be read in
exactly the same order it was written to the file
10-71
Some Methods in the Class ObjectInputStream
(Part 2 of 5)
10-72
Some Methods in the Class ObjectInputStream
(Part 3 of 5)
10-73
Some Methods in the Class ObjectInputStream
(Part 4 of 5)
10-74
Some Methods in the Class ObjectInputStream
(Part 5 of 5)
10-75
Checking for the End of a Binary File the
Correct Way
All of the ObjectInputStream methods that read
from a binary file throw an EOFException when
trying to read beyond the end of a file
This can be used to end a loop that reads all the data in a file
10-76
The Serializable Interface
In order to make a class serializable, simply add
implements Serializable to the
heading of the class definition
public class SomeClass implements Serializable
10-77
The Serializable Interface
When any of these objects is written to the same stream
more than once, then after the first time, only its serial
number is written, instead of the object full description ,
which is very efficient
10-78
The Serializable Interface
When a serializable class has instance variables of a
class type (that is composition), then all those classes
must also be serializable
10-79
Binary I/O of Objects
If a class is serializable, objects of this class can also be input to
and output from a binary file
The writeObject() method of the ObjectOutputStream class
is used to write an object to a binary file
The readObject() method of the ObjectInputStream class is
usewd to read an object from a binary file
In order to use the value returned by readObject() as an object of a
class, it must be type cast first:
Car c1 = (Car)objectInputStream.readObject();
It is best to store the data of only one class type in any one file
Storing objects of multiple class types or objects of one class type mixed
with primitives can lead to loss of data
See FileIO14.java
10-80
Array Objects in Binary Files
Since an array is an object, arrays can also be read and
written to binary files using readObject and
writeObject
If the base type is a class, then it must also be serializable, just
like any other class type
10-81
Random Access to Binary Files
The streams for sequential access to files are the
ones most commonly used for file access in Java
10-82
Random Reading and Writing to Files
All reads and writes take place starting at the file pointer location
10-83
Random Reading and Writing to Files
10-84
Opening a File
The constructor for RandomAccessFile takes
either a string file name or an object of the class File
as its first argument
10-85
Pitfall: A Random Access File Need Not Start
Empty
If the file already exists, then when it is opened, the
length is not reset to 0, and the file pointer will be
positioned at the start of the file
This ensures that old data is not lost, and that the file pointer
is set for the most likely position for reading (not writing)
10-86
Some Methods of the Class RandomAccessFile
(Part 1 of 7)
10-87
Some Methods of the Class RandomAccessFile
(Part 2 of 7)
10-88
Some Methods of the Class RandomAccessFile
(Part 3 of 7)
10-89
Some Methods of the Class RandomAccessFile
(Part 4 of 7)
10-90
Some Methods of the Class RandomAccessFile
(Part 5 of 7)
10-91
Some Methods of the Class RandomAccessFile
(Part 6 of 7)
10-92
Some Methods of the Class RandomAccessFile
(Part 7 of 7)
10-93