Download Binary File I/O

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
Introduction
to Programming
Lecture 34
File I/O -Part 2
Today’s Learning Objectives


To understand the File Class
To Learn how to use the classes
ObjectOutputStream and ObjectInoutStream to
write and read binary files
SE15: File I/O Part 2
31–2
Lecture Outline




The File Class
Binary file I/O
Object I/O with object Streams
Using the Serializable interface
Savitch Chapter 9
SE15: File I/O Part 2
31–3
Using the File Class

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”);
if(! fileObject.canRead())
System.out.println(“Not allowed to read that
file”);
SE15: File I/O Part 2
31–4
Some Methods of the File Class




exists()
boolean canRead()
boolean canWrite()
boolean delete()



long length()
String getName()


tries to delete the file
Returns the name of the file (not path)
String getPath()

Returns the path name
SE15: File I/O Part 2
31–5
Binary File I/O




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:


ObjectInputStream
ObjectOutputStream
SE15: File I/O Part 2
31–6
Opening a binary file for writing

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.
new FileOutputStream(“out.dat”)

ObjectOutputStream outputStream = new
ObjectOutputStream(new
FileOutputStream(“out.dat”));
SE15: File I/O Part 2
31–7
Writing primitive types to binary files

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.
SE15: File I/O Part 2
31–8
Writing Strings to binary files

writeUTF(String aString)


UTF refers to a particular encoding of the string, you
therefore need to use the readUTF method to read it
back from the file.


writes the String value of aString to the output stream
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
31–9
Opening a binary file for reading

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.
new FileInputStream(“mydata.dat”)


ObjectInputStream inputStream = new
ObjectInputStream(new
FileInputStream(“mydata.dat”));
If your program attempts to read beyond the end of the file a
EOFException is thrown.
SE15: File I/O Part 2
31–10
Reading and Writing Objects

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.

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)

Reading and writing objects also requires import java.io.*
SE15: File I/O Part 2
31–11
The Serializable Interface

What is the effect of making a class serializable?




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.
SE15: File I/O Part 2
31–12
Summary


Looked at the File Class
Looked at examples of reading and writing binary files
containing



Primitive types
Strings
Objects using the Serializable interface
SE15: File I/O Part 2
31–13
Follow-up Work

Savitch chapter 9
SE15: File I/O Part 2
31–14