Solved

problem saving test data to a file and then reloading it

Posted on 2003-11-04
6
257 Views
Last Modified: 2010-03-31
I am having problems saving data from my HolidayPlannerTester program to file and then reloading it. The program compiles fine but when running the exception message is shown 'save failed'.

Here is my code please post if other code is required.

import java.io.*;

class ObjectFileUtility {

/* Writes an object to the named file. */
public static void saveObject(Serializable obj, String filename) throws IOException

{
ObjectOutputStream oos =
new ObjectOutputStream( new FileOutputStream(filename) );

oos.writeObject(obj);
oos.flush();
oos.close();
}
public static Object loadObject(String filename) throws IOException
{
ObjectInputStream ois =
new ObjectInputStream(
new FileInputStream(filename)
);

try
{
Object obj = ois.readObject();
ois.close();
return obj;
}
catch (ClassNotFoundException e) {
throw new IOException("ClassNotFoundException");
}
}
}

\/\/\/\/\/\/\/\/\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
import java.util.*;

class HolidayPlanner implements java.io.Serializable {
private String companyId;
private HashMap holidayRecords;

public HolidayPlanner(String companyId)
{
this.companyId =  companyId;
holidayRecords = new HashMap();
}

public void addHolidayRecord( HolidayRecord h)
{
String emp = h.getEmployeeID();
holidayRecords.put(emp,h);
}

public void removeHolidayRecord(String employeeID)
{

holidayRecords.remove(employeeID);
}

public HolidayRecord getHolidayRecord(String employeeID)
{
return (HolidayRecord)holidayRecords.get(employeeID);
}

public String toString()

{
return "  Collection for   " +  companyId + " :\n  " + holidayRecords;
}
}

\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
import java.util.*;
class HolidayPlannerTester
{
public static void main(String [] args)
{
HolidayPlanner p = new HolidayPlanner("8788");
HolidayRecord h1 = new HolidayRecord ("1",14);
p.addHolidayRecord(h1);
HolidayRecord h2 = new HolidayRecord ("2",14);
p.addHolidayRecord(h2);
HolidayRecord h3 = new HolidayRecord ("3",7);
p.addHolidayRecord(h3);
HolidayRecord h4 = new HolidayRecord ("4",10);
p.addHolidayRecord(h4);
HolidayRecord h5 = new HolidayRecord ("5",23);
p.addHolidayRecord(h5);
HolidayRecord h6 = new HolidayRecord ("6",7);
p.addHolidayRecord(h6);
HolidayRecord h7 = new HolidayRecord ("7",21);
p.addHolidayRecord(h7);
HolidayRecord h8 = new HolidayRecord ("8",23);
p.addHolidayRecord(h8);
System.out.println(p);
try {
ObjectFileUtility.saveObject(p, "HolidayPlanner.object");
System.out.println("Planner saved.");
}
catch (java.io.IOException e) {
System.out.println("Save failed. " + e.getMessage());
}
}
}
0
Comment
Question by:k1ngp1n99
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9678961
>>but when running the exception message is shown 'save failed'.

An the rest of the message is..?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9678975
Change the e.getMessage() to e.toString() and you'll see the following:

Save failed. java.io.NotSerializableException: HolidayRecord

You need to make HolidayRecord implement the Serializable interface.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 250 total points
ID: 9679058
Remember that if you go down this route and then you want to make changes to the HolidayRecord later, there is a good chance that you won't be able to read in the file because the object signature will have changed.

With the application you have at the moment, I would recommend using DataOutputStream/DataInputStream to create and access the file.  Unless you need to use Serializable as part of the project ;-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:k1ngp1n99
ID: 9679063
Yep that worked thanks for the help again Jimmack ;)

0
 

Author Comment

by:k1ngp1n99
ID: 9679065
Yep that worked thanks for the help again Jimmack ;)

0
 
LVL 15

Expert Comment

by:jimmack
ID: 9679083
;-)

No problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now