2013-02-23 36 views
-1

我有一个很大的问题,我在一个txt文件中写入相同的对象帐户当我点击查看所有对象我已经写在TXT是只显示我最后一个,在JOptionPane中显示帐户,第二个是我如何使帐户采取一,二,三......每个帐户,我写了一个数字加一先前帐户。 我believ解释清楚为什么给我看最后一个对象,我把它放在TXT

if(str.equals("Receipt")) 
{ 
     ObjectInputStream in = null; 
     Account acc = null; 
     try 
     { 
      in = new ObjectInputStream(new FileInputStream("Accounts.txt")); 
      while((acc = (Account) in.readObject()) !=null) 
      { 
       if (acc instanceof Account) 
        ((Account)acc).print(); 
       //acc.print(); 
      } 
     } 
     catch(EOFException ex) 
     { 
      System.out.println("End of file reached."); 
     } 
     catch(ClassNotFoundException ex) 
     { 
      System.out.println("Error casting"); 
      ex.printStackTrace(); 
     } 
     catch(FileNotFoundException ex) 
     { 
      System.out.println("Error specified file does not exist"); 
      ex.printStackTrace(); 
     } 
     catch(IOException ex) 
     { 
      System.out.println("Error with I/O processes"); 
      ex.printStackTrace(); 
     } 
     finally 
     { 
      try 
      { 
       in.close(); 
      } 
      catch(IOException ex) 
      { 
       System.out.println("Another IOException during the closing"); 
       ex.printStackTrace(); 
      } 
     } 
} 

代码写入对象文件

Account ac=new Account(posoOfil,poso,ariLoga,aitiol,diafor); 
     ac.print(); 

     try{ 

     OutputStream file = new FileOutputStream("Accounts.txt"); 
     OutputStream buffer = new BufferedOutputStream(file); 
     ObjectOutput output = new ObjectOutputStream(buffer); 
     try{ 
     output.writeObject(ac); 
     output.flush(); 

System.out.println("Object written to file"); 
     } 
     finally{ 
     output.close(); 
     } 
    } 
     catch 
(FileNotFoundException ex) {System.out.println("Error with specified file") ; 
ex.printStackTrace();} 

    catch(IOException ex){ 
     System.out.println("Cannot perform output."); 
    } 

     } 

和类账户

public class Account implements Serializable { 

    private int arithmKat; 
// private Date date ; 
    private int posoOfil; 
    private int posoKat; 
    private String ariLoga ; 
    private String aitiol; 
    private boolean diafor=false; 

    public Account(int posoOfil, int posoKat,String ariLoga,String aitiol,boolean diafor){ 
    arithmKat++; 
    this.posoOfil=posoOfil; 
    this.posoKat=posoKat; 
    this.ariLoga=ariLoga; 
    this.aitiol=aitiol; 
    this.diafor=diafor; 

    } 
    void print(){ 
    System.out.println(arithmKat+" "+posoOfil+" "+posoKat+" "+diafor);} 

} 
+0

请编辑您的问题以设置您的代码的格式。目前无法读取。 – 2013-02-23 09:32:52

+0

请重新格式化您的代码,以便它可以真正被人读取。 – Henry 2013-02-23 09:32:57

+2

这实际上花了很多努力。 >。> – Achrome 2013-02-23 09:36:50

回答

0

你只写一个帐户文件。在编写帐户的代码中根本没有循环。编写几个帐户的代码如下所示:

private void writeAccounts(List<Account> accounts) throws IOException { 
    OutputStream file = new FileOutputStream("Accounts.txt"); 
    OutputStream buffer = new BufferedOutputStream(file); 
    ObjectOutput output = new ObjectOutputStream(buffer); 

    try { 
     for (Account account : accounts) {   
      output.writeObject(account); 
     } 
     output.close(); 
    } 
    finally { 
     output.close(); 
    } 
} 
+0

我把循环? 与for或while? – 2013-02-23 10:33:18

+0

如果您有多个帐户可供编写,您应该打开输出流,通过您的帐户循环并写入每个帐户,然后关闭输出流。无论你使用一段时间还是一段时间,都是无关紧要的。如你所愿。 – 2013-02-23 10:35:20

+0

我不知道我在哪里做循环。 – 2013-02-23 10:50:19

相关问题