Reading appended objects from file in java

hamed_91

Honorable
Jul 17, 2012
2
0
10,510
0
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
0
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.
 
Thread starter Similar threads Forum Replies Date
Neon The (Anthro) Umbreon Apps General Discussion 3
P Apps General Discussion 3
M Apps General Discussion 3
W Apps General Discussion 1
K Apps General Discussion 2
J Apps General Discussion 4
B Apps General Discussion 1
A Apps General Discussion 1
A Apps General Discussion 1
T Apps General Discussion 1
D Apps General Discussion 10
H Apps General Discussion 4
C Apps General Discussion 2
kol12 Apps General Discussion 2
G Apps General Discussion 1
N Apps General Discussion 1
K Apps General Discussion 1
H Apps General Discussion 2
W Apps General Discussion 4
Z Apps General Discussion 2

ASK THE COMMUNITY