Solved

problem saving test data to a file and then reloading it

Posted on 2003-11-04
6
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 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