Results 1 to 4 of 4
  1. #1
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Serialization example, reading the data back into the object

    I'm trying to create a class which can store its data in a serialized format to disk, and then load the serialized data back into itself.

    I have an incomplete code here:

    Java Code:
    package example;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class Test implements Serializable {
    
    	private static int[] items = new int[10];
    	private static int location = 0;
    
    	public void addValue(int val) {
    		items[location] = val;
    		location = (location + 1) % 10;
    	}
    
    	public void printAll() {
    		for (int i = 0; i < 10; i++) {
    			if (items[i] != 0) {
    				System.out.println(items[i]);
    			}
    		}
    	}
    
    	public void save() {
    		try {
    			FileOutputStream FOS = new FileOutputStream("data.ser");
    			ObjectOutputStream OOS = new ObjectOutputStream(FOS);
    
    			OOS.writeObject(this);
    
    			OOS.close();
    			FOS.close();
    		} catch (FileNotFoundException f) {
    			//
    			f.printStackTrace();
    		} catch (IOException i) {
    			//
    			i.printStackTrace();
    		}
    	}
    
    	public void load() {
    		try {
    			FileInputStream FIS = new FileInputStream("data.ser");
    			ObjectInputStream OIS = new ObjectInputStream(FIS);
    
    			Object temp = OIS.readObject();
    
    			OIS.close();
    			FIS.close();
    		} catch (FileNotFoundException f) {
    			//
    			f.printStackTrace();
    		} catch (IOException i) {
    			//
    			i.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			//
    			e.printStackTrace();
    		}
    	}
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Test t = new Test();
    		t.addValue(14);
    		t.addValue(5);
    		t.printAll();
    		t.save();
    		t.addValue(32);
    		t.load();
    		System.out.println("Loading the same data above");
    		t.printAll();
    	}
    }
    I don't know how to proceed from here. Bascially, I don't know how to use the data I've read from the serialized file into the temp object, to update the Test instance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    first, remove the static in
    Java Code:
    	
    private static int[] items = new int[10];
    private static int location = 0;
    but you could make the load method static and return the Test instance:

    Java Code:
    	public static Test load() throws IOException, ClassNotFoundException  { //or use try catch here...
    			FileInputStream FIS = new FileInputStream("data.ser");
    			ObjectInputStream OIS = new ObjectInputStream(FIS);
    			Test temp = (Test) OIS.readObject();
    			OIS.close();
    			FIS.close();
    			return temp;
    	}
    and call t = Test.load();

    (btw: read the naming conventions :D )

  3. #3
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks for your answer, but this way, I need to reassign T variable:

    t = Test.load();

    However, what I really want to do is to just call a method of "t" instance and it'll load itself. Is that possible?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    if i understand you correctly, then: no! :)

    but indirectly, in your example, you could serialize only the integer array! in other cases, you could write a wrapper or something similar to that maybe ?!

Similar Threads

  1. Replies: 1
    Last Post: 11-09-2010, 09:28 AM
  2. Prevent default write object serialization
    By nwboy74 in forum Advanced Java
    Replies: 5
    Last Post: 06-10-2010, 10:30 PM
  3. Object serialization
    By sky in forum New To Java
    Replies: 15
    Last Post: 11-23-2009, 01:10 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Serialization - writing/reading to memory
    By ajeeb in forum Advanced Java
    Replies: 3
    Last Post: 01-27-2009, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •