A Guide to Programming in Java

computerharpySoftware and s/w Development

Dec 2, 2013 (3 years and 8 months ago)

81 views

© 2007 Lawrenceville Press

Slide
1

Chapter 12

Files


A file is a collection of related data stored on a
persistent medium such as a hard disk or a CD


Files often store data used by an application


Files also store data generated by an application


Files can be read from and written to by more than
one application

© 2007 Lawrenceville Press

Slide
2

Chapter 12

The File Class


Part of the java.io package


The File class objects create new files, test for the
existence of files, and delete files


File methods include:


createNewFile()


delete()


exists()

© 2007 Lawrenceville Press

Slide
3

Chapter 12

Handling Exceptions


An exception is an error affecting program execution


An exception handler is a block of code that performs
an action when an exception occurs


Exception handlers are required when calling certain
methods


The
try
-
catch
-
finally

statement can be used to
write an exception handler

© 2007 Lawrenceville Press

Slide
4

Chapter 12

The File Stream


A stream processes characters and is implemented
with a class


The file stream keeps track of the file position and
performs sequential access


When thinking about file operations, the file should be
visualized as a stream of data. The carriage return
character (Cr) followed by a line feed character (Lf) is
called a line terminator. A
-
1 is the end
-
of
-
file:

© 2007 Lawrenceville Press

Slide
5

Chapter 12

The FileReader and BufferedReader
Classes


Part of the java.io package


The FileReader class is used to create an input file
stream and includes methods:


close()


The BufferedReader class is used to read text from
the stream and includes methods:


read()


readLine()


close()

© 2007 Lawrenceville Press

Slide
6

Chapter 12

Processing Numeric Data


A file on disk is a set of characters, even when the
file contains numeric data


Class methods in the Double and Integer classes can
be used to convert string data to numeric data


Double class methods include:


parseDouble()


Integer class methods include:


parseInteger()

© 2007 Lawrenceville Press

Slide
7

Chapter 12

The FileWriter and BufferedWriter Classes


Part of the java.io package


The FileWriter class is used to create an output file
stream and includes methods:


close()


The BufferedWriter class is used to send text to the
stream and includes methods:


newLine()


write()


close()

© 2007 Lawrenceville Press

Slide
8

Chapter 12

Storing Objects in a File


Writing objects to a file is called object serialization


Reading objects from a file is called object
deserialization


Reading and writing objects requires object input and
output streams


Objects that are to be written to a file must
implement the Serializable interface


Reading an object from a file requires casting

© 2007 Lawrenceville Press

Slide
9

Chapter 12

The FileOutputStream and
ObjectOutputStream Classes


Part of the java.io package


The FileOutputStream class is used along with the
ObjectOutputStream class to write objects to a file


The FileOutputStream class includes methods:


close()


The ObjectOutputStream class includes methods:


writeObject()


writeInt()


writeDouble()


close()

© 2007 Lawrenceville Press

Slide
10

Chapter 12

The FileInputStream and
ObjectInputStream Classes


Part of the java.io package


The FileInputStream class is used along with the
ObjectInputStream class to read objects from a file


The FileInputStream class includes methods:


close()


The ObjectInputStream class includes methods:


readObject()


readInt()


readDouble()


close()