从一个文件读取一个对象到一个ArrayList

问题描述:

当我试图读取一个对象并存储在arraylist但即时获取异常这是代码的一部分,即时通讯面临的一个问题。从一个文件读取一个对象到一个ArrayList

public class Customer implements Serializable { 

private String username; 
private String password; 
private int age; 
private String accttype; 
private String acctno; 
private float amount; 

Customer() { 
    System.out.println("Im in Customer"); 
} 

public boolean writeToDataBase(String uname, String pwd, int cage, String caccttype, String cacctno, float camount) throws IOException { 

    Customer custobj = new Customer(); 
    FileOutputStream fos=null; 
    ObjectOutputStream oos=null; 
    custobj.username = uname; 
    custobj.password = pwd; 
    custobj.age = cage; 
    custobj.accttype = caccttype; 
    custobj.acctno = cacctno; 
    custobj.amount = camount; 
    try { 

     fos=new FileOutputStream("Customerdetails.txt",true); 
     oos=new ObjectOutputStream(fos); 
     oos.writeObject(custobj); 
     oos.close(); 
     fos.close(); 
     return true; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     return false; 
    } 
    finally 
    { 
     fos.close(); 
     oos.close(); 
    } 
} 

public boolean retriveFromDataBase(int a) throws IOException 
{ 
    try { 
     Customer custobj = new Customer(); 
     FileInputStream fis=null; 
     ObjectInputStream ois=null; 
     ArrayList<Customer> custlist; 
     try { 
      custlist = new ArrayList<Customer>(); 
      fis = new FileInputStream("Customerdetails.txt"); 
      ois = new ObjectInputStream(fis); 
      while (fis.available()!=0) { 
       custobj=(Customer)ois.readObject(); 
       custlist.add(custobj); 
      } 
      System.out.println("Customer List" + custlist.size()); 
      if (a == 3) { 
       for (int i = 0; i < custlist.size(); i++) { 
        custobj = custlist.get(i); 
        custobj.displayCustomers(); 
       } 
      } 
      return true; 

     } catch (Exception ex) { 
      System.out.println(ex.toString()); 
      System.out.println("No users are presnt in the file"); 
      return false; 
     } 
     finally 
     { 
      ois.close(); 
      fis.close(); 
     } 
    } 
    catch(Exception ex) 
    { 
     System.out.println(ex.toString()); 
     return false; 
    } 
} 
public void displayCustomers() 
{ 
    try 
    { 
     System.out.println("details"+username+"\t"+age+"\t"+password+"\t"+acctno+"\t"+accttype+"\t"+amount); 
    } 
    catch(Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 
} 
+2

你在哪里得到例外?请发布堆栈跟踪 – 2011-02-13 03:54:56

+1

异常包含有关问题原因的信息。你不应该忽视它们。如果您的具体问题是您不了解异常和堆栈跟踪,则应该将它们包含在问题中,以便人们可以为您解释它。另外,请用`ex.printStackTrace()`或者更好的方法`throw ex`替换`System.out.println(ex.toString())`,以便获得更多关于问题原因的信息。 – BalusC 2011-02-13 04:37:20

您的对象是否实现Serializiable或Externalizeable接口?如果是的话,你是否使用了不能实现serializible/externalizeable的非传递对象,并且不提供无自变量的默认构造函数?

没有进一步的信息(哪些例外,更多的代码)很难说。

我注意到,当你第二次运行它时,程序会抛出java.io.StreamCorruptedException异常。它只运行一次就可以正常工作。

问题在于,每次在writeToDatabase(..)方法中序列化时,都无法APPEND到同一个文件:Customerdetails.txt。因此,在writeToDatabase(..)方法中调用FileOutputStream的构造函数时,请删除append标志:“true”。