Java Streams   «Prev  Next»

Lesson 7File streams
Objective Understand the purpose and use of file streams.
Up until now, most of the examples have involved System.out and System.in. These are convenient examples of output and input streams.
However, in real work you will more commonly be attaching streams to other data sources like files and network connections.
The java.io.FileInputStream and java.io.FileOutputStream classes are concrete subclasses of java.io.InputStream and java.io.OutputStream, respectively, that provide input and output streams connected to particular files. These classes have all the usual methods of their superclasses such as
  1. read(),
  2. write(),
  3. available(),
  4. flush().
These methods are used exactly as they are for any other input or output stream. The only methods of significance which are new are constructors:

public FileInputStream(String filename) throws IOException
  
public FileOutputStream(String filename) throws IOException

Java7 NIO 2

ObjectOutputStream

The java.io.ObjectOutputStream and java.io.ObjectInputStream classes are considered to be higher-level classes in the java.io package, and as we learned earlier, that means that you will wrap them around lower-level classes, such as java.io .FileOutputStream and java.io.FileInputStream. Here is a small program that creates a (Cat) object, serializes it, and then deserializes it:
import java.io.*;
class Cat implements Serializable { } // 1
public class SerializeCat {
 public static void main(String[] args) {
  Cat c = new Cat(); // 2
  try {
   FileOutputStream fs = new FileOutputStream("testSer.ser");
   ObjectOutputStream os = new ObjectOutputStream(fs);
   os.writeObject(c); // 3
   os.close();
  } 
  catch (Exception e) { e.printStackTrace(); }
  try {
   FileInputStream fis = new FileInputStream("testSer.ser");
   ObjectInputStream ois = new ObjectInputStream(fis);
   c = (Cat) ois.readObject(); // 4
   ois.close();
  } 
  catch (Exception e) { e.printStackTrace(); }
 }
}

Let us take a look at the key points in this example:
  1. We declare that the Cat class implements the Serializable interface. Serializable is a marker interface; it has no methods to implement. (In the next several sections, we will cover various rules about when you need to declare classes Serializable.)
  2. We make a new Cat object, which as we know is serializable.
  3. We serialize the Cat object c by invoking the writeObject() method. It took a fair amount of preparation before we could actually serialize our Cat. First, we had to put all of our I/O-related code in a try/catch block. Next we had to create a FileOutputStream to write the object to. Then we wrapped the FileOutputStream in an ObjectOutputStream, which is the class that has the magic serialization method that we need. Remember that the invocation of writeObject() performs two tasks: it serializes the object, and then it writes the serialized object to a file.
  4. We deserialize the Cat object by invoking the readObject() method.
    The readObject() method returns an Object, so we have to cast the deserialized object back to a Cat.