Download COMP1681 / SE15 Introduction to Programming

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
COMP1681 / SE15
Today’s Learning Objectives
Introduction
to Programming


To understand the File Class
To Learn how to use the classes
ObjectOutputStream and ObjectInoutStream to
write and read binary files
Lecture 34
File I/O -Part 2
SE15: File I/O Part 2
Using the File Class
Lecture Outline




31–2
The File Class
Binary file I/O
Object I/O with object Streams
Using the Serializable interface

The File class is a wrapper class for file names. The constructor
takes a String as an argument and produces an object that can be
thought of as a file with that name.

It also allows you to test properties of the file such as if a file exists
or if you have permission to read it.
File fileObject = new File(“myfile.txt”);
if(! fileObject.exists())
System.out.println(“No file by that name”);
Savitch Chapter 9
if(! fileObject.canRead())
System.out.println(“Not allowed to read that
file”);
SE15: File I/O Part 2
31–3
Some Methods of the File Class









tries to delete the file
long length()
String getName()


Files are stored in the same form as they are in the
computer's memory.
This means they are efficient (compact)
Binary files created by a Java program can be moved
between computers and still read by a Java program –
but only a Java program.
The preferred classes for processing binary files are:

Returns the name of the file (not path)

String getPath()

31–4
Binary File I/O
exists()
boolean canRead()
boolean canWrite()
boolean delete()


SE15: File I/O Part 2
ObjectInputStream
ObjectOutputStream
Returns the path name
SE15: File I/O Part 2
31–5
SE15: File I/O Part 2
31–6
Opening a binary file for writing
Writing primitive types to binary files

Opening a binary file for writing is similar to text files

The ObjectOutputStream class has no constructor that takes
a file name as its argument so we use the class
FileOutputStream to create a stream that can be used as an
argument to the ObjectOutputStream constructor e.g.

Once a stream class is connected to a file, you can write various
primitive types to a file using the methods of
ObjectOutputStream.
 e.g. outputStream.writeInt(n);
 which writes the variable integer variable n a file.

There are similar methods:
 writeDouble(double n)
 writeFloat(float n)
 writeChar(int n)
 writeBoolean(boolean b)

You can mix up the types you write but you need to know the order
in which they appear in the file to be able to read them back again.
new FileOutputStream(“out.dat”)

ObjectOutputStream outputStream = new
ObjectOutputStream(new
FileOutputStream(“out.dat”));
SE15: File I/O Part 2
31–7
Writing Strings to binary files



writes the String value of aString to the output stream

Again this is similar to text files

The ObjectInputStream class has no constructor that
takes a file name as its argument so we use the class
FileInputStream to create a stream that can be used as
an argument to the ObjectInputStream constructor e.g.
UTF refers to a particular encoding of the string, you
therefore need to use the readUTF method to read it
back from the file.

new FileInputStream(“mydata.dat”)
outputStream.writeUTF(“Yikes it’s Java”);

UTF stands for Unicode Text Format

This encodes all Unicode characters, but favours the ASCII
character set, giving these short efficient codes.

SE15: File I/O Part 2


ObjectInputStream inputStream = new
ObjectInputStream(new
FileInputStream(“mydata.dat”));
If your program attempts to read beyond the end of the file a
EOFException is thrown.
31–9
SE15: File I/O Part 2
31–10
The Serializable Interface
Reading and Writing Objects

31–8
Opening a binary file for reading
writeUTF(String aString)

SE15: File I/O Part 2
You can read and write objects to a binary file using the:
writeObject method of the ObjectOuputStream class
and the
readObject method of the ObjectInputStream class.

What is the effect of making a class serializable?


This requires the class to Serializable by adding implements
Serializable to the heading of the class definition
public class Library implements Serializable {
The Serializable interface is unusual in that it does not require
any methods to be implemented (it is just a signal to the compiler)


Java assigns a serial number to each object it writes to a
stream.
If the object is written more than once then after the first
time Java only writes the serial number
This makes I/O more efficient and files smaller
If a Serializable class has instance variables of a class type
then the class for the instance variables must also be
Serializable and so on for all levels of instance variables
within classess.
Reading and writing objects also requires import java.io.*
SE15: File I/O Part 2
31–11
SE15: File I/O Part 2
31–12
Summary


Follow-up Work
Looked at the File Class
Looked at examples of reading and writing binary files
containing




Savitch chapter 9
Primitive types
Strings
Objects using the Serializable interface
SE15: File I/O Part 2
31–13
SE15: File I/O Part 2
31–14