[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

saving and loading

Posted on 2005-04-17
5
Medium Priority
?
180 Views
Last Modified: 2010-03-31
The code bellow will save the departments into a file.

public static void SavingDepartments()
{
    System.out.println("Enter the name of the file");
    String filename = SavitchIn.readLine();
                   
    try
    {
         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
                            
         Vector dept = cpn.getDepartments();
                          
         if(dept.isEmpty())
         {
              System.out.println("There is no departments in the company");
         }
         else
         {
      Iterator iter = dept.iterator();
                                
      while(iter.hasNext())
      {
                oos.writeObject(iter.next());           }  
                                
      oos.flush();
      oos.close();         
          }                           
     }
     catch(IOException ioe)
    {
         System.out.println("error in writing");
    }
}


The code bellow will load the departments and display.

public static void LoadingDepartments()
{
      System.out.println("Enter the file name");
      String filename = SavitchIn.readLine();
                   
      Department d = null;
      ArrayList list = new ArrayList();
                   
      try
      {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));         
                   
             try
             {
       d = (Department) ois.readObject();
       list.add(d);
             }
             catch(IOException ioe)
             {
       d = null;
             }
                         
             while(d!=null)
             {
        try
        {
           d = (Department) ois.readObject();
          list.add(d);
        }
        catch(IOException ioe)
        {
           d = null;
           System.out.println("EOF reached");         
        }
             }
                         
             ois.close();
                   
 }
 catch(FileNotFoundException fnfe)
{
   System.out.println("File " + filename + " not found");
   return;
}
catch(IOException ioe)
{
   System.out.println("Error in reading");
}
catch(ClassNotFoundException cnfe)
{
  System.out.println(cnfe.getMessage());
}

Iterator iter = list.iterator();
             
while(iter.hasNext())
{
     d = (Department) iter.next();
     System.out.println(d);
}
                   
}


The main problem is 1 department can have many employees. So when I save the department it should also save the employees. When I try to save the department that has employees, this msg is displayed System.out.println("error in writing");. Now when Im loading, nothing is displayed. It should display the department as well as the employee details.

How do I solve this problem?.

You could download the code from:

www.mutaiyas.com/db/Employee.zip

The codes stated above can be found in driver.java

Your help is kindly appreciated.

Regards

Eugene
0
Comment
Question by:eugene007
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 240 total points
ID: 13801953
Department and Employee should be declared as implementing Serializable
0
 

Author Comment

by:eugene007
ID: 13801978
what is the purpose of the serializable interface?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802043
To allow objects to be written with ObjectOutputStream and read with ObjectInputStream
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 80 total points
ID: 13803763
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13807783
:-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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 …
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

825 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