Sign in with
Sign up | Sign in
Your question

Reading appended objects from file in java

Last response: in Applications
Share
July 17, 2012 8:32:36 AM

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.");
}



}
}


}

July 17, 2012 5:56:00 PM

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.
a b L Programming
July 18, 2012 2:28:25 AM

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?
!