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