-1

I want to write and read a serializable class from a object file. I can write this object to a file but I cannot seem to be able to read the object back in and write it to the console.

Neither one of these options works. I just want to read in the object as it was written to the object file.

Code Tried:

public void readBinary1() throws IOException, ClassNotFoundException {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream
                                      ("G:\\testobject.tmp"));
        input.readObject();

        System.out.println(new myClass());
    }

    public void readBinary1() throws IOException, ClassNotFoundException {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream
                                      ("G:\\testobject.tmp"));

        System.out.println(new myClass(input.readObject()));
    }

class:

class myClass implements Serializable {
    myClass() {}

    myClass(myClass b) {
       this.a = b.a;
       this.b = b.b;
    }

    private static final long serialVersionUID = 1L;
    private int a = 0;
    private int b = 100;
    private transient int c = 50;
}

Code Added:

    I had to add a toString to my class to do it the way that it was suggested to do. That seems to be the way that is easiest in the short run but I would rather write the object and then be able to read in the object with out having to use the toString. Is there a way that I can read in the object with one read and then be able to break the info apart with the .dot notation. Such as mc.variable1 and mc.variable2 and so on. I had to type cast the read object also before the code would compile.

There are couple of input and output streams that allow for the serialization of objects to a file. I am certain that there are different ways to wrap the classes for the read and was wondering also which way was the best way to create the read.

Doug Hauf
  • 3,025
  • 8
  • 46
  • 70
  • 1
    Try searching for this problem on stack overflow -- there are literally dozens of similar questions and answers. – Software Engineer Feb 17 '14 at 22:37
  • see http://stackoverflow.com/questions/17293991/how-to-write-and-read-java-serialized-objects-into-a-file – michael Feb 17 '14 at 22:39
  • You don't seem to understand you're reading a *serialized object* from the stream. `MyClass mc = (MyClass) input.readObject();`. Done. – Brian Roach Feb 17 '14 at 22:40
  • 1
    And see http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html – michael Feb 17 '14 at 22:40
  • 1
    I know that I am reading a serialized object. I think that it reads the whole object into the variable and then I have to break that information up into what I would want to do with it like write or print or whatever. If I read an object from a file it should read it all in at one time just like if I was reading one string from the file. It would read a string at a time. – Doug Hauf Feb 17 '14 at 22:51

1 Answers1

1

Do this:

myClass readedObject = (myClass) input.readObject();
System.out.println(readedObject);
Luis Alves
  • 1,286
  • 12
  • 32