You are not logged in. Please login at www.codechef.com to post your questions!

×

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)

asked 28 Dec '16, 07:44

vpriyanshu40's gravatar image

2★vpriyanshu40
815
accept rate: 12%


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.

link

answered 28 Dec '16, 08:18

arpit728's gravatar image

2★arpit728
6831149
accept rate: 10%

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;

    objectOutputStream.writeObject(person);
    objectOutputStream.close();


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

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

    objectInputStream.close();

    System.out.println(personRead.name);
    System.out.println(personRead.age);
}                                                                                                                 }

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.

link

answered 28 Dec '16, 09:15

only4's gravatar image

4★only4
1.5k210
accept rate: 17%

edited 28 Dec '16, 09:23

@vpriyanshu40

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.

link

answered 28 Dec '16, 10:52

arpit728's gravatar image

2★arpit728
6831149
accept rate: 10%

edited 28 Dec '16, 10:53

Thank you @arpit728

(28 Dec '16, 11:08) vpriyanshu402★

@vpriyanshu40

please accept and upvote my answer if it helped.

(28 Dec '16, 11:26) arpit7282★

@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.

link

answered 28 Dec '16, 10:26

vpriyanshu40's gravatar image

2★vpriyanshu40
815
accept rate: 12%

Answer is hidden as author is suspended. Click here to view.

answered 21 Feb '17, 15:40

deepakkuamre's gravatar image

0★deepakkuamre
(suspended)
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,166

question asked: 28 Dec '16, 07:44

question was seen: 576 times

last updated: 21 Feb '17, 15:40