Results 1 to 4 of 4
- 11-17-2010, 04:03 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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:
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.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(); } }
- 11-17-2010, 04:20 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
first, remove the static in
but you could make the load method static and return the Test instance:Java Code:private static int[] items = new int[10]; private static int location = 0;
and call t = Test.load();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; }
(btw: read the naming conventions :D )
- 11-17-2010, 05:40 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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?
- 11-17-2010, 05:47 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Need to Save a data to file from JPanel and read the data back to Jpanel
By yashrajsen in forum AWT / SwingReplies: 1Last Post: 11-09-2010, 09:28 AM -
Prevent default write object serialization
By nwboy74 in forum Advanced JavaReplies: 5Last Post: 06-10-2010, 10:30 PM -
Object serialization
By sky in forum New To JavaReplies: 15Last Post: 11-23-2009, 01:10 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Serialization - writing/reading to memory
By ajeeb in forum Advanced JavaReplies: 3Last Post: 01-27-2009, 03:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks