Object input/output streams in java

What are ObjectInputStream and ObjectOutputStream in java. I am having a problem in a source file saying

Exception in thread “main” java.io.InvalidClassException: Block; invalid descriptor for field
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:710)
at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:828)

ObjectInputStream and ObjectOutputStream are used for deserialization and serialization in java. In serialization we save the state of an object to a file. And in deserialization we retrieve object from the previously saved state. This is heavily used when you want to transfer object over a network.
You can only serialize or deserialize the object of those class that implement Serializable interface. The reason for your error is probably because you are trying to serialize or deserialize the object that doesn’t implement Serializable interface.


The Java ObjectInputStream class (java.io.ObjectInputStream) enables you to read Java objects from an InputStream instead of just raw bytes. You wrap an InputStream in a ObjectInputStream and then you can read objects from it. Of course the bytes read must represent a valid, serialized Java object. Otherwise reading objects will fail.

Normally we use the ObjectInputStream to read objects written (serialized) by a Java ObjectOutputStream.

ObjectInputStream Example

Here is a Java ObjectInputStream example:

ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream("object.data"));
MyClass object = (MyClass) objectInputStream.readObject();                                      objectInputStream.close();

Here’s a good example of both ObjectInputStreams and ObjectOutputStreams

import java.io.*;                                                                                              public class ObjectInputStreamExample {

public static class Person implements Serializable {
    public String name = null;
    public int    age  =   0;

public static void main(String[] args) throws IOException, ClassNotFoundException {

    ObjectOutputStream objectOutputStream =
        new ObjectOutputStream(new FileOutputStream("data/person.bin"));

    Person person = new Person();
    person.name = "Jakob Jenkov";
    person.age  = 40;


    ObjectInputStream objectInputStream =
        new ObjectInputStream(new FileInputStream("data/person.bin"));

    Person personRead = (Person) objectInputStream.readObject();


}                                                                                                                 }

I am not sure about your error. It might possible that any two class versions are exactly identical.

And I think this source file is networking related. That’s all I can help.

1 Like

@arpit728 and @only4 I want to master java but when I look at the codes they seem horrible. Java is not easy as c++. So any suggestions.


Every thing that we are not familiar with seems to be horrible at first. You might have some idea about c++ and therefore it seems to be easy to you. To master anything the only thing that is required is good resources. I can tell you the best resources to learn java, but problem with the resources that I am going to share with you is that you will get addicted to java (don’t blame me then) like any TV series. Trust me java is very easy and playful, but only when you understand it.

If you really want to master java then this series will help you. The teacher in this series is too versed in java and teaches every concept in detail with example and analogies that are very simple to understand.

See this link, here I have posted best source to learn java language.

1 Like

Thank you @arpit728


please accept and upvote my answer if it helped.

People do not lack strength; Dr DC Shetty it