GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - ObjectInputStream Class
Selected Reading
Java - ObjectInputStream ClassIntroductionThe Java ObjectInputStream class deserializes primitive data and objects previously written using an ObjectOutputStream. Following are the important points about BufferedInputStream −
Class declarationFollowing is the declaration for Java.io.ObjectInputStream class − public class ObjectInputStream
extends InputStream
implements ObjectInput, ObjectStreamConstants
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Usage of ObjectInputStream available() methodThe following example shows the usage of Java ObjectInputStream available() method. ObjectInputStreamDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
try {
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeUTF("Hello World");
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// check how many bytes are available
System.out.println("" + ois.available());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− 13 Example - Closing an ObjectInputStream After Reading an ObjectThe following example shows the usage of Java ObjectInputStream close() method. This example writes an object to a file, reads it using ObjectInputStream, and then closes the stream. ObjectInputStreamDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class ObjectInputStreamDemo {
public static void main(String[] args) {
try {
// Serialize object to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat"));
oos.writeObject(new Person("Alice", 30));
oos.close();
// Read object from file
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"));
Person person = (Person) ois.readObject();
System.out.println("Read Object: " + person);
// Close the ObjectInputStream
ois.close();
// Trying to read again after closing
System.out.println("Trying to read again...");
ois.readObject(); // This will throw an IOException
} catch (IOException | ClassNotFoundException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Read Object: Person{name='Alice', age=30}
Trying to read again...
Exception: Stream Closed
Explanation
Example - Using defaultReadObject() for Basic SerializationThe following example shows the usage of Java ObjectInputStream defaultReadObject() method. This example demonstrates default serialization using defaultReadObject() ObjectInputStreamDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject(); // Restores default serialized fields
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class ObjectInputStreamDemo {
public static void main(String[] args) {
try {
// Serialize object
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat"));
oos.writeObject(new Person("Alice", 30));
oos.close();
// Deserialize object
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"));
Person person = (Person) ois.readObject();
ois.close();
System.out.println("Deserialized Object: " + person);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Deserialized Object: Person{name='Alice', age=30}
Explanation
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |