Solved

problem saving test data to a file and then reloading it

Posted on 2003-11-04
6
246 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
Comment Utility
>>but when running the exception message is shown 'save failed'.

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

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:k1ngp1n99
Comment Utility
Yep that worked thanks for the help again Jimmack ;)

0
 

Author Comment

by:k1ngp1n99
Comment Utility
Yep that worked thanks for the help again Jimmack ;)

0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
;-)

No problem.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 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

15 Experts available now in Live!

Get 1:1 Help Now