2015-06-05 65 views
0

如何從文件中讀取文件數據/反序列化對象。我創建了一個二進制文件文件,其中包含公司數據列表,我可以添加新公司及其相關數據,但是何時我想讀回的所有文件DATAS它只給人的第一家公司DATAS和它打印空..what低於這個問題是我做了什麼反序列化對象只讀取一個數據

public class CompanyInfo extends Company { 
    int counter=0; 
    Scanner in=new Scanner(System.in); 
    private ArrayList<Company> companyinfo; 

public CompanyInfo() { 
    companyinfo=new ArrayList<Company>(); 
} 

public void registercompany() { 

    System.out.println("Enter Company Name \n"); 
    companyName=in.nextLine(); 
    System.out.println("\n"); 
    System.out.println("Enter Company Code \n"); 
    companyCode=in.nextLine(); 
    System.out.println("\n"); 
    System.out.println("Enter the Share Number \n"); 
    shareNo=in.nextInt(); 
    System.out.println("\n"); 
    System.out.println("Enter Closing Rate \n"); 
    closingRate=in.nextDouble(); 
    Company cin=new Company(companyName,companyCode,shareNo,closingRate); 
    companyinfo.add(cin); 

    try { 
     ObjectOutputStream outObjFile =new ObjectOutputStream(new FileOutputStream("companies.dat",true)); 
     Company company = new Company(companyName,companyCode,shareNo,closingRate); 
     outObjFile.writeObject(company); 
     outObjFile.writeChars("\n"); 
     outObjFile.close(); 

    } catch (Exception e) { 
     // TODO: handle exception 
     System.out.println("A file error has occurred. Sorry."); 
     System.out.println(e.getMessage()); 
    } 


    counter++; 

    } 
public void viewcompany(){ 
    try { 
     ObjectInputStream inObjFile = new ObjectInputStream(
       new FileInputStream("companies.dat")); 
       System.out.println(inObjFile.readObject()); // displays first object 
       Company company = (Company)inObjFile.readObject(); // restores object 
       System.out.println(company); // displays restored object 
       inObjFile.close(); // finished with the file now. 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 
+0

你只序列化/反序列化的公司,如果你想獲得所有的人,你應該序列化/反序列化'companyinfo' –

+0

我沒有得到你對不起!你能解釋一下嗎? –

+1

我猜''append = true'標誌到'FileOutputStream',可能會試圖將每個寫入值附加到文件。但是,ObjectOutputStream將特殊的標記值寫入流中,因此如果要從一個文件中讀取多個單獨的對象流,則必須做更多的工作。 –

回答

0

我會做兩種不同的方式。

  1. 將所有細節添加到hashmap中並序列化該對象。所以在回讀時,我可以使用「鍵」進行搜索。密鑰可以是公司的名稱或ID。你也可以對ArrayList做同樣的事情。搜索可能很困難。 Immutable collections from Guava使用的內存更少,更好。
  2. 另一種解決方案是使用key.dat創建目標文件並將其放入名爲company的目錄中。所以很容易回讀並進行搜索。
0

終於讓我找到了答案...希望它可以幫助別人too..good運氣

public class CompanyInfo extends Company { 
    int counter=0; 
    private static String filename = "company.dat"; 
    Scanner in=new Scanner(System.in); 
    private ArrayList<Company> companyinfo; 

    public CompanyInfo() { 
     companyinfo=new ArrayList<Company>(); 
    } 

public void registercompany() { 


    System.out.println("Enter Company Name \n"); 
    companyName=in.nextLine(); 
    System.out.println("Enter Company Code \n"); 
    companyCode=in.nextLine(); 
    System.out.println("Enter the Share Number \n"); 
    shareNo=in.nextInt(); 
    System.out.println("Enter Closing Rate"); 
    closingRate=in.nextDouble(); 
    Company cin=new Company(companyName,companyCode,shareNo,closingRate); 
    companyinfo.add(cin); 
File file=new File(filename); 
boolean append=true; 
ObjectOutputStream out=null; 
try { 
    if (!file.exists()||!append) { 
     out=new ObjectOutputStream(new FileOutputStream(filename)); 
    } 
    else 
    { 
     out=new AppendableObjectOutputStream(new FileOutputStream (filename, append)); 
    } 
    Company company = new Company(companyName,companyCode,shareNo,closingRate); 
    out.writeObject(company); 
    out.flush(); 

} catch (Exception e){ 
    e.printStackTrace(); 
}finally{ 
    try{ 
     if (out != null) out.close(); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

    counter++; 

    } 
public void viewcompany(){ 
    try { 
     List<Object> results = new ArrayList<Object>(); 
     FileInputStream fis = new FileInputStream(filename); 
     ObjectInputStream ois = new ObjectInputStream(fis); 
     try{ 
     while (true) { 
     results.add(ois.readObject());} 
      } 
     catch (OptionalDataException e) { 
      if (!e.eof) throw e; } 
     finally { 
      System.out.println(results); 
      //System.out.println(((Company)results.get(0)).companyName); 
      ois.close(); } 
    } 
    catch (Exception e){ 
     System.out.println(e.getMessage()); 
    } 

} 
private static class AppendableObjectOutputStream extends ObjectOutputStream { 
    public AppendableObjectOutputStream(OutputStream out) throws IOException { 
     super(out); 
    } 

    @Override 
    protected void writeStreamHeader() throws IOException {} 
    } 
}