Results 1 to 2 of 2
Thread: Serializing objects
- 04-03-2011, 07:00 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Serializing objects
I'm trying to serialize java objects but i keep getting a list of errors. my program accepts multiple values and creates an instance of a class with them. the created object then gets stored in a StorageSystem class... it looks something like this
Then in the storage system i add the stored object to an arrayList... what i also want to do is add the object to a serialized file... my method for doing this is below... Am i going about this the wrong way. thanks...Java Code:aCD = new CD(title, artist, playTime, numOfTracks); store.addItem(aCD);
This is the entire class:Java Code:public void addItem(Item hold) // adds object to the ArrayList { itemList.add(hold); totalStored++; try { FileOutputStream fout = new FileOutputStream("thequeue.ser"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(hold); oos.close(); } catch (Exception e) { e.printStackTrace(); } }
Java Code:package jukebox; import java.util.ArrayList; import java.io.*; public class StorageSystem implements Serializable { ArrayList<Item> itemList = new ArrayList<Item>(); // sets up an array list private int totalStored; // keeps track of the number of stored items public StorageSystem(int totalStored) // constructor, gets set to 0 { this.totalStored = totalStored; } public void addItem(Item hold) // adds object to the ArrayList { itemList.add(hold); totalStored++; try { FileOutputStream fout = new FileOutputStream("thequeue.ser"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(hold); oos.close(); } catch (Exception e) { e.printStackTrace(); } } }
- 04-03-2011, 07:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Serializing
By shashanksworld in forum New To JavaReplies: 7Last Post: 03-29-2010, 04:47 PM -
serializing pdf files
By windie in forum New To JavaReplies: 0Last Post: 03-11-2009, 04:46 PM -
Object not serializing
By MamboBanda in forum New To JavaReplies: 1Last Post: 08-12-2008, 12:15 AM -
Serializing Arrays
By javaplus in forum New To JavaReplies: 3Last Post: 11-08-2007, 09:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks