Solved

problem saving test data to a file and then reloading it

Posted on 2003-11-04
6
274 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
jar file executable 12 58
iterator/ListIterator approach 17 39
How to convert from xls to xlsx using java 7 55
jsp insert to database example 2 45
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
This video teaches viewers about errors in exception handling.

856 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