2014-02-18 26 views
-1
import java.io.FileInputStream; 
    import java.io.FileOutputStream; 

    try { 
     FileInputStream inFile_Customer = new FileInputStream("database\\Customers.bin"); 
     ObjectInputStream ois_Customer = new ObjectInputStream(inFile_Customer); 
     customerList = (ArrayList<Customer>)ois_Customer.readObject(); 

     FileInputStream inFile_Package = new FileInputStream("database\\Packages.bin"); 
     ObjectInputStream ois_Package = new ObjectInputStream(inFile_Package); 
     packageList = (ArrayList<Packages>)ois_Package.readObject(); 

     FileInputStream inFile_Order = new FileInputStream("database\\Orders.bin"); 
     ObjectInputStream ois_Order = new ObjectInputStream(inFile_Order); 
     orderList = (ArrayList<Order>)ois_Order.readObject(); 

     FileInputStream inFile_Invoice = new FileInputStream("database\\Invoices.bin"); 
     ObjectInputStream ois_Invoice = new ObjectInputStream(inFile_Invoice); 
     invoiceList = (ArrayList<Invoice>)ois_Invoice.readObject(); 

    } catch (ClassNotFoundException |IOException | ClassCastException e) { 
     e.printStackTrace(); 
    } 


    public void WriteCustomer(){ 
    try { 
     FileOutputStream fos = new FileOutputStream("database\\Customers.bin"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(customerList); 
     oos.close(); 
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } 
    } 

    public void WritePackage(){ 
    try { 
     FileOutputStream fos = new FileOutputStream("database\\Packages.bin"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(packageList); 
     oos.close(); 
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } 
    } 

    public void WriteOrder(){ 
     try { 
     FileOutputStream fos = new FileOutputStream("database\\Orders.bin"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(orderList); 
     oos.close(); 
     } catch(Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 

    public void WriteInvoice(){ 
     try { 
     FileOutputStream fos = new FileOutputStream("database\\Invoices.bin"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(invoiceList); 
     oos.close(); 
     } catch(Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 

這是我的編碼。它沒有顯示任何錯誤,但它不能工作。信息寫入文件內。但它不能讀取裏面的文件。請幫幫我。我是新來的Java。非常感謝。如果您要求檢查,我可以向您發送我的完整編碼。謝謝。FileInputStream&FileOutputStream。無法獲取文件中的信息

+1

定義'不能工作'和'不能讀取裏面的文件'。 – EJP

回答

0

這裏是一個應該工作的例子:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 

public class Test { 

    public static class Customer implements Serializable { 
     private static final long serialVersionUID = 1L;   

     private String name; 

     public Customer() { 
     }  

     public Customer(String name) { 
      this.name = name; 
     }  

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     @Override 
     public String toString() { 
      return "Customer{" + "name=" + name + '}'; 
     }     
    } 

    private ArrayList<Customer> customerList = new ArrayList<>(); 

    public void writeCustomers(String path) { 
     ObjectOutputStream oos_Customer = null; 
     try { 
      oos_Customer = new ObjectOutputStream(new FileOutputStream(path)); 
      oos_Customer.writeObject(customerList); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } finally { 
      if (oos_Customer != null) { // old-style safe closing when done 
       try { 
        oos_Customer.close(); 
       } catch (IOException e) {      
       } 
      } 
     } 
    } 

    public void readCustomers(String path) { 
     try (ObjectInputStream ois_Customer = new ObjectInputStream(new FileInputStream(path))) { // new-style safe closing when done 

      customerList = (ArrayList<Customer>) ois_Customer.readObject(); 

     } catch (ClassNotFoundException | IOException | ClassCastException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 

     final Test t = new Test(); 
     t.customerList.add(new Customer("A")); 
     t.customerList.add(new Customer("B")); 

     System.out.println("Initial customer list: " + t.customerList); 

     t.writeCustomers("Customers.bin"); 

     t.customerList.clear(); 

     t.readCustomers("Customers.bin"); 

     System.out.println("Read customer list: " + t.customerList); 
    } 
} 

一些其他注意事項:

  • 確保你的類實現了java.io.Serializable並已成立的serialVersionUID任何恆長價值。這將幫助你,如果你改變了課程,然後閱讀一箇舊的文件
  • 使用包裝模式「新SomeStream(新SomeOtherStream(...))」。通常不需要有兩個不同的變量,而不是一個指向外部流的變量。當你關閉外部流時,它會關閉鏈中的所有流。
    • 通常,您的代碼看起來是正確的。只要確保你指定輸入和輸出文件相同的路徑,並確保您檢查的ArrayList正確的實例,它讀出後:)

如果仍然不能爲你工作,發佈請提供整個代碼。

相關問題