The Artima Developer Community
Sponsored Link

Java Answers Forum
How do I save an array list of serialized objects?

5 replies on 1 page. Most recent reply: Sep 14, 2003 7:55 PM by wdzwdz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Laurence Bier

Posts: 8
Nickname: sandman99
Registered: Sep, 2003

How do I save an array list of serialized objects? Posted: Sep 11, 2003 7:48 PM
Reply to this message Reply
Advertisement
I currently trying this approach, because a more straightforward one keeps failing. A file is created, but no data is ever retrieved:

The save:

private void SaveCustomerData()
{

// customerDataList is the ArrayList
// used to save customer data to disk

try
{

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(FILE_NAME));
for(int i=0;i<customerDataList.size();i++)
{
CustomerData customerDataItem=(CustomerData) customerDataList.get(i);
out.writeObject(customerDataItem);
}

out.close();

}

catch(Exception e)
{



}


}

The load:

private void GetCustomerDataFromDisk()
{

// used to retrieve customer data from disk

boolean errorTriggered=false;

try
{

ObjectInputStream in=new ObjectInputStream(new FileInputStream(FILE_NAME));
CustomerData[] customerDataItems=(CustomerData[]) in.readObject();
for(int i=0;i<customerDataItems.length;i++)
AddCustomerFromDisk(customerDataItems.getCustomerFirstName(),customerDataItems.getCustomerLastName(),customerDataItems.getCustomerID(),customerDataItems.ge tSavingsAccount(),customerDataItems.getCheckingAccount(),customerDataItems.get InvestmentAccount(),customerDataItems.getLineOfCreditAccount());

in.close();


}


...

etc.

}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: How do I save an array list of serialized objects? Posted: Sep 11, 2003 10:27 PM
Reply to this message Reply
You are writing object by object to the file. Therefore when you are reading them back you have to read object by object. Therefore
CustomerData[] customerDataItems=(CustomerData[]) in.readObject();

is not going to work. Since you are reading it trying to cast to a CustomerData[] array. It will fail at that point I guess.

One more thing when you said the straight approach failed what was your straight forward approach?

Laurence Bier

Posts: 8
Nickname: sandman99
Registered: Sep, 2003

Re: How do I save an array list of serialized objects? Posted: Sep 13, 2003 7:48 PM
Reply to this message Reply
My original try is below. I get an "incompatible types" error on the read.


// writing the data
// customerDataList is an arraylist

try
{

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(FILE_NAME));

out.writeObject(customerDataList);

out.close();

}

catch(Exception e)
{



}

// reading the data


try
{

ObjectInputStream in=new ObjectInputStream(new FileInputStream(FILE_NAME));
customerDataList=in.readObject();
in.close();


}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: How do I save an array list of serialized objects? Posted: Sep 13, 2003 9:18 PM
Reply to this message Reply
customerDataList=(ArrayList)in.readObject();


Why don't you try this?

Laurence Bier

Posts: 8
Nickname: sandman99
Registered: Sep, 2003

Re: How do I save an array list of serialized objects? Posted: Sep 14, 2003 4:00 PM
Reply to this message Reply
Excellent! Very nice work - and so obvious!

wdzwdz

Posts: 3
Nickname: wdzwdz
Registered: Dec, 2002

Re: How do I save an array list of serialized objects? Posted: Sep 14, 2003 7:55 PM
Reply to this message Reply
//My example!
package objectserial.array;

import java.io.Serializable;

public class WDZObject implements Serializable {
public static void main(String[] args) {
}
public WDZObject(String sName,String sSex,int iAge){
this.name= sName;
this.age = iAge;
this.sex = sSex;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String toString(){
return "name="+name+",sex="+sex+",age="+age;
}

private String name="wdz";
private int age=10;
private String sex;

}
////
package objectserial.array;

import java.io.*;
import java.util.*;

public class ArraySerial {
private String FILE_NAME = "e:/sss.txt";
private ArrayList array;

public ArraySerial() {
array = new ArrayList();
}

public void loadFromDisk() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
FILE_NAME));
array = (ArrayList)in.readObject();
}
catch (Exception e) {
}
}
public void printArrayList(){
for(int i =0;i<array.size();i++){
System.out.println("object "+i+"="+array.get(i));
}
}

private void SaveToDisk() {
WDZObject wdzObject = null;
for (int i = 0; i < 20; i++) {
wdzObject = new WDZObject("wdz"+i,"man", 80);
array.add(wdzObject);
}
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
FILE_NAME));
/* out.writeObject(array);ºÍÏÂmainde ´úÂëµÈ¼Û
for (int i = 0; i < array.size(); i++) {
WDZObject customerDataItem = (WDZObject) array.get(i);
out.writeObject(customerDataItem);
}*/
out.writeObject(array);
out.close();
}

catch (Exception e) {
System.out.println("SaveToDisk error--"+e.getMessage());
}
}

public static void main(String[] args) {
ArraySerial a = new ArraySerial();
a.SaveToDisk();
a.loadFromDisk();
a.printArrayList();
}
}

Flat View: This topic has 5 replies on 1 page
Topic: share global hash map across multiple threads Previous Topic   Next Topic Topic: web server v/s application server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use