ObjectInputStream
In this chapter you will learn:
- How to use ObjectInputStream
- How to read data through ObjectOutputStream
- Object serialization and ObjectInputStream
Use ObjectInputStream
The ObjectInput
interface extends the DataInput
interface.
The ObjectInputStream
class extends the
InputStream
class and implements the ObjectInput
interface.
ObjectInputStream
is responsible for reading objects from a stream.
ObjectInputStream
supports object serialization through
the readObject( )
method.
readObject( )
method is called to deserialize an object.
A constructor of this class is
ObjectInputStream(InputStream inStream) throws IOException
inStream
is the input stream from which serialized objects should be read.
Read through ObjectOutputStream
The following code show to use ObjectOutputStream
to read and write primitive data.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
/* j a va2s.c o m*/
public class Main {
static final String dataFile = "data.dat";
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
out.writeObject(new BigInteger("123"));
out.writeInt(123);
out.writeUTF("a String");
} finally {
out.close();
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
try {
while (true) {
BigInteger price = (BigInteger) in.readObject();
int unit = in.readInt();
String desc = in.readUTF();
System.out.println(price);
System.out.println(unit);
System.out.println(desc);
}
} catch (EOFException e) {
}
} finally {
in.close();
}
}
}
The output:
Read object
The following code shows how to use the ObjectInputStream
and
ObjectOutputStream
to do the object serialization.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
//from ja va 2s. c om
public class Main {
public static void main(String args[]) throws Exception{
MyClass object1 = new MyClass("Hello", -1, 2.1);
System.out.println("object1: " + object1);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
MyClass object2 = (MyClass) ois.readObject();
ois.close();
System.out.println("object2: " + object2);
}
}
class MyClass implements Serializable {
String s;
int i;
double d;
public MyClass(String s, int i, double d) {
this.s = s;
this.i = i;
this.d = d;
}
public String toString() {
return "s=" + s + "; i=" + i + "; d=" + d;
}
}
Next chapter...
What you will learn in the next chapter:
- How to use DataInputStream
- Read with DataInputStream
- Use underline BufferedInputStream
- DataOutputStream and network stream
Home » Java Tutorial » Reader Writer