Follows The Java I/O Model
Follows The Java I/O Model
Follows The Java I/O Model
There are specialized versions of these object types that are used with files.
File I/O 1
File: a class that deals with the properties of files (not the contents of files).
file name and directory some generic attributes (java is OS independent!) the file length (number of bytes in the file).
A File object is used to get information about files (and directories/folders), but not to read from or write to files.
File I/O
Writing to a File
File I/O
String s = fin.readLine();
Object Streams
Any serializable object can be written to and read from an object stream
can be used to save objects in a file for later use can also be used to exchange Java objects over a network (Interprocess Communication).
Recall that to make your own class serializable, you need to implement the interface Serializable.
unless you don't want a deep copy, you don't need to do anything special, just state that the class implements Serializable.
File I/O 7
ObjectInputStream: allows you to read objects (serialized objects) from any byte stream.
ObjectInputStream ois = new ObjectInputStream( new FileInputStream("foo")); Object huh = ois.readObject(); ObjectOutputStream: allows you to write objects to any byte stream.
Sample Code
SerializeArgs: reads/writes an array of String (whatever it gets in args). SerializePlay: defines it's own serializable class and saves/restores an array of these objects. SerializePoly: defines a base class and some derived classes. Creates an array of these objects and serializes it saving to a file.
Poly-Poly-Poly-Polymorphism
File I/O 9
HW3!