Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

saving my class types into file

Posted on 1998-11-23
3
Medium Priority
?
188 Views
Last Modified: 2010-03-30
i have read that with the class: java.io.ObjectOutputStream, i can write to and from a file without the need to implement the writing and reading functions , but only with the: .readObject and .writeObject.
if i created my own class, can i use these methods (and how)?
0
Comment
Question by:rzvika2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 200 total points
ID: 1228033
The is called object serialization. This technique allows you to turn the state of an object
into a stream of bytes (that you can store in a file on disk, send through a network, etc.),
and later recover the original object (with its original state of course). The condition
on the class you want to serialize is that it implements java.io.Serializable. Also, if you
are referring to other objects, they also have to implement Serializable (otherwise, the
serialization will fail and you will have a java.io.NotSerializableException). Some classes
of the JDK already implement Serializable. This is the case, for example, of Vector and
Hashtable.

Sample code:

public class IntegerStore implements java.io.Serializable {

    private int integer = 0;

    public class IntegerStore() {
    }

    public int getInteger() {
       return integer;
    }

    public void setInteger(int integer) {
        this.integer = integer;
        return;
    }

    public static void main(String args[])  throws Exception {
        IntegerStore  store = new IntegerStore ();
        store.setInteger(5);

        // serialization + storing in "integerstore.ser"
        FileOutputStream out = new FileOutputStream("integerstore.ser");
        BufferedOutputStream bufOut = new BufferedOutputStream(out);
        ObjectOutputStream objOut = new ObjectOutputStream(bufOut);
        objOut.writeObject(store);
        objOut.close();        

        // now, we recover the serialized object!
        FileInputStream in = new FileInputStream("integerstore.ser");
        BufferedInputStream bufIn = new BufferedInputStream(in);
        ObjectInputStream objIn = new ObjectInputStream(bufIn);
        IntegerStore recoveredStore = (IntegerStore )objIn.readObject();
        objIn.close();

        System.out.println("The value was " + recoveredStore.getInteger() + "!");
        return;
    }
}
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1228034
Note: you will have to import java.io.* to compile the code above. Here is an article that
should interest you:

http://www.javaworld.com/javaworld/jw-05-1997/jw-05-persistence.html
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1228035
I see that there is a typing error in the code. I repost it entirely for clarity.
An IntegerSore object is built (called "store" in the example). It holds an
integer value that we set to 5. We then serialize it and store it in the
"integerstore.ser" file. Finally, we read back this file, and recover its
content as an IntegerStore. We print out the value it holds to be sure
that we do not deal with an IntegerStore created "from scratch" (as
we would have if we were building the "recoveredStore" instance
using "new IntegerStore()"). As we read "5", and not "0", we have
proven that the state of the object was saved in the file and that we
were able to recover it.

import java.io.*;

   public class IntegerStore implements java.io.Serializable {

       private int integer = 0;

       public IntegerStore() {
       }

       public int getInteger() {
          return integer;
       }

       public void setInteger(int integer) {
           this.integer = integer;
           return;
       }

       public static void main(String args[])  throws Exception {
           IntegerStore  store = new IntegerStore ();
           store.setInteger(5);

           // serialization + storing in "integerstore.ser"
           FileOutputStream out = new FileOutputStream("integerstore.ser");
           BufferedOutputStream bufOut = new BufferedOutputStream(out);
           ObjectOutputStream objOut = new ObjectOutputStream(bufOut);
           objOut.writeObject(store);
           objOut.close();        

           // now, we recover the serialized object!
           FileInputStream in = new FileInputStream("integerstore.ser");
           BufferedInputStream bufIn = new BufferedInputStream(in);
           ObjectInputStream objIn = new ObjectInputStream(bufIn);
           IntegerStore recoveredStore = (IntegerStore )objIn.readObject();
           objIn.close();

           System.out.println("The value was " + recoveredStore.getInteger() + "!");
           return;
       }
   }
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question