Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Serializable : readObject and writeObject

Thread view: 
Joseph KHOURY - 14 Jan 2006 05:51 GMT
Hello,

I have 3 classes Personne, entreeb==>FileInputStream ,
sortieb==>FileOutputStream.
The code works one time. The problem is when I add serialized new objects
( launch again the code sortieb) I can't read all objects (entreeb) specific
a new objects added.

have you an idea?

Thanks form help

-----------------------------------------------------------
-----------------------------------------------------------
//Class Personne
import java.io.*;
class Personne implements Serializable {
private String nom;
private int age;
public Personne(String n, int a){
   nom = n;
   age = a;
   }
public void aff() {
   System.out.println (nom + "   "  + age);
   }
}

------------------------------------------------
//Flux in
import java.io.*;
import java.util.*;
public class entreeb {
public static void main(String[] args){
Personne p;
int i=0;
try {
  FileInputStream fist= new FileInputStream("Personne.ser");
  ObjectInputStream oist = new ObjectInputStream(fist);
  try {
   while (true)
    { System.out.println("i="+i);
     p = (Personne) oist.readObject();
     p.aff();
     i++;
    }
  } catch (EOFException e) { }
  oist.close();
 } catch (Exception e) {
  System.out.println("Exception loading  personne : " + e.getMessage());
 }
}
}

------------------------------------------------------------------
//Flux out
import java.io.*;
import java.util.*;
public class sortieb {
public static void main(String[] args) throws IOException {
Personne p = new Personne("name", 55);

File f=new File("Personne.ser");
FileOutputStream fost= new FileOutputStream(f,true);
ObjectOutputStream oost = new ObjectOutputStream(fost);
int i=0;
while (i<5) {
   System.out.println("i = "+i+ "p="+p);
   oost.writeObject(p);
   i+=1;
   }
oost.close();
}
}
Roedy Green - 14 Jan 2006 05:57 GMT
>FileOutputStream fost= new FileOutputStream(f,true);

This is most likely your problem.  Try writing all in one sweep.
don't append.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.