2013-01-01 102 views
0

我需要將存儲在數組中的數據傳輸到一個文件,在這種情況下,我創建的文件爲emplo.dat。我無法找到正確的方法來做到這一點。因爲它是一個對象數組,所以使用readObject傳輸對象不起作用。要傳輸文件的對象數組

import java.io.*; 
import employee.*; 

public class ClearTechSolutions { 
    public static void main(String[] args) { 
     ContractEmployee[] con = new ContractEmployee[3]; 
     PermanentEmployee[] per = new PermanentEmployee[3]; 

     for(int i=0;i<3;i++) { 
      con[i] = new ContractEmployee(); 
      per[i] = new PermanentEmployee(); 
     } 

     con[0].setData("C001", "Mohan", "E-32 M.G Marg", "30/Jun/1974", 0.0f, 1000.0f, 0.0f,  5000.0f, 20, 7); 
     con[1].setData("C002", "Steve", "A-32 M.G. Marg", "15/Oct/1981", 0.0f, 1500.0f, 0.0f,  7500.0f, 22, 3); 
     con[2].setData("C003", "Mary", "A-31 Rohini", "15/Dec/1979", 0.0f, 2500.0f, 0.0f, 10000.0f,  18, 5); 
     per[0].setData("E001", "Bob", "E-12 Lajpat Nagar", "01/Feb/1974", 0.0f, 20000.0f, 0.0f,  800000.0f, 7, 28); 
     per[1].setData("E002", "Kevin", "E-15 Mandir Marg", "01/Apr/1990", 0.0f, 25000.0f, 0.0f,  1000000.0f, 6, 26); 
     per[2].setData("E003", "Mohan", "E-15 Mandir marg", "31/July/1984",0.0f, 10000.0f, 0.0f,  400000.0f,9,30); 
     con[0].calc(1000.0f); 
     con[1].calc(1500.0f); 
     con[2].calc(2500.0f); 
     per[0].calc(20000.0f); 
     per[1].calc(25000.0f); 
     per[2].calc(10000.0f); 

     try { 
      File file = new File("emplo.dat"); 
      file.createNewFile(); 
      FileOutputStream fi = new FileOutputStream(file); 
      ObjectOutputStream obj = new ObjectOutputStream(fi); 

      for(int i=0;i<3;i++) { 
       System.out.println("\n"); 
       System.out.println("The details for Contract Employee "+(i+1)+" are:"); 
       con[i].print(); 
      } 
      for(int i=0;i<3;i++) { 
       System.out.println("\n"); 
       System.out.println("The details for Permanent Employee "+(i+1)+" are:"); 
       per[i].print(); 
      } 
     } 
     catch(IOException e) {} 
    } 
} 
+2

基元數組也是一個對象。 – Mob

+0

http://www.javacoffeebreak.com/articles/serialization/index.html – ethrbunny

+0

'傳輸到文件'意味着寫入文件? –

回答

1
import java.io.*; 
    import employee.*; 

    public class ClearTechSolutions { 
     public static void main(String[] args) { 
      ContractEmployee[] con = new ContractEmployee[3]; 
      PermanentEmployee[] per = new PermanentEmployee[3]; 

      for(int i=0;i<3;i++) { 
       con[i] = new ContractEmployee(); 
       per[i] = new PermanentEmployee(); 
      } 

      con[0].setData("C001", "Mohan", "E-32 M.G Marg", "30/Jun/1974", 0.0f, 1000.0f, 0.0f,  5000.0f, 20, 7); 
      con[1].setData("C002", "Steve", "A-32 M.G. Marg", "15/Oct/1981", 0.0f, 1500.0f, 0.0f,  7500.0f, 22, 3); 
      con[2].setData("C003", "Mary", "A-31 Rohini", "15/Dec/1979", 0.0f, 2500.0f, 0.0f, 10000.0f,  18, 5); 
      per[0].setData("E001", "Bob", "E-12 Lajpat Nagar", "01/Feb/1974", 0.0f, 20000.0f, 0.0f,  800000.0f, 7, 28); 
      per[1].setData("E002", "Kevin", "E-15 Mandir Marg", "01/Apr/1990", 0.0f, 25000.0f, 0.0f,  1000000.0f, 6, 26); 
      per[2].setData("E003", "Mohan", "E-15 Mandir marg", "31/July/1984",0.0f, 10000.0f, 0.0f,  400000.0f,9,30); 
      con[0].calc(1000.0f); 
      con[1].calc(1500.0f); 
      con[2].calc(2500.0f); 
      per[0].calc(20000.0f); 
      per[1].calc(25000.0f); 
      per[2].calc(10000.0f); 

      try { 
       File file = new File("emplo.dat"); 
       file.createNewFile(); 
       FileOutputStream fi = new FileOutputStream(file); 
       ObjectOutputStream obj = new ObjectOutputStream(fi); 

       for(int i=0;i<3;i++) { 
        System.out.println("\n"); 
        System.out.println("The details for Contract Employee "+(i+1)+" are:"); 
        con[i].print(); 
obj.writeObject(con[i]);    <-- this will write your object to file connected to file output stream. 
       } 
       for(int i=0;i<3;i++) { 
        System.out.println("\n"); 
        System.out.println("The details for Permanent Employee "+(i+1)+" are:"); 
        per[i].print(); 
obj.writeObj(per[i]); 
       } 
      } 
      catch(IOException e) {} 
     } 
    } 

從文件

ObjectInputStream ois=new ObjectInputStream(fin); 
abc temp; 
    while((temp=(abc)ois.readObject())) 
{ 
    System.out.println(temp); 
    } 

讀到這裏是ABC類名要檢索其對象。在文件輸入流對象中查找。

+0

哦,忘了提及.. 你的類必須實現Serializable才能使用序列化的概念。 – Arpit

+0

非常感謝:D – Robin

1

正如ethrbunny所建議的,您應該瞭解更多關於序列化的知識。

有問題的問題,您應該

  1. 保證類ContractEmployee和PermanentEmployee都 實現Serializable接口。
  2. 與用ObjectOutputStream您創建,調用obj.writeObject(con)obj.writeObject(per)寫入陣列 到文件中舉辦的需要數據「目標文件」 實例。

請注意,您將一個對象數組寫入文件,而不是一個一個地寫入各個對象。所以期望在反序列化的時候還能得到一組對象。