Reading appended objects from file in java

Status
Not open for further replies.

hamed_91

Honorable
Jul 17, 2012
2
0
10,510
Hello,
I am researching way to "reading appended objects from file in java".
but my code just reading first object from file and application cannot read remainder objects from file and in this time EXCEPTION occurred.
Please represent me the useful algorithm for reading ALL appended objects from file.
Thanks.

Best Regard
Hamed...


private ObjectInputStream input;

private ObjectOutputStream output;
private FileOutputStream fos;
private Contact c;


public void add(){

Contact c1;//instance from serializable class "Contact"
String name;
String family;
String number;
Boolean append=true;

File file=new File("contact.ser");
if(!file.exists())
append=false;


try{

fos=new FileOutputStream("contact.ser",append);
output=new ObjectOutputStream(fos);

}
catch (IOException ex) {
System.out.println("Error opening file.") ;

}



Scanner x=new Scanner(System.in);
System.out.println("Enter name;");
name=x.nextLine();
System.out.println("Enter family;");
family=x.nextLine();
System.out.println("Enter number;");
number=x.nextLine();

c1=new Contact(name,family,number);



try{
output.writeObject(c1);
}
catch(IOException ioex){
System.err.println("Error writing to file.");
}

try{

if(output!=null){
output.flush();
output.close();

}


}
catch(IOException io){
System.err.println("Error closing file.");
}


}


public void read(){


Contact con;

int i=0;

Object obj=null;

File file=new File("contact.ser");
if(file.exists()){
try{

FileInputStream fis=new FileInputStream("contact.ser");

input=new ObjectInputStream(fis);

}
catch(IOException ioEx){
System.err.println("Error opening file.");
}
try{
while(true){
con=(Contact)input.readObject();
System.out.println(con.getName()+"1");
System.out.println(con.getFamily()+"2");
System.out.println(con.getNumber()+"3");
System.out.println();
}

}
catch(EOFException end){
return;

}
catch(ClassNotFoundException cl){
System.err.println("Unable to create object.");
}
catch(IOException ioEx){
System.err.println("Error during read from file."+ioEx.getMessage()+ioEx.toString());
ioEx.getStackTrace();

}
try{
if(input!=null){

input.close();
}
}
catch(IOException ioEx){
System.err.println("Error closing file.");
}



}
}


}

 

hamed_91

Honorable
Jul 17, 2012
2
0
10,510
The exception occurred when cal of method read():
in WHILE loop,always first object downcast to MyObject(Contact) and name,family & number of this contact, print in command line. but always when application try to downcast next objects to Contact, IOException occurred. :pfff:

Thanks for your attention.
 

randomizer

Champion
Moderator
You shouldn't call readObject() in a loop. As far as I know it always reads the whole file in one go and then processes it and the only way to append objects is as described on the page that I linked to above. Then again, if you really want to add multiple objects to the file, why not just serialise a collection of objects instead?

As for the exception, you'll need to provide more details. There are lots of places that could throw IOExceptions in this code. Do you have the actual exception message and stacktrace?
 
Status
Not open for further replies.