2014-12-06 39 views
0

以前分配的每个元素,我已经写了一个数组列表数据到所谓的(ItStateBinary.dat)爪哇 - 读从二进制文件的ArrayList集合,并在新的数组

的二进制文件,现在我想读的数组列表从二进制文件中,然后将arraylist中的每个元素分配给数组。

到目前为止我有此:

public CarOwner[] readListFromBinary() throws Exception 
{ 
    String fileName = "ItStateBinary.dat"; 
    FileInputStream inStream = new FileInputStream(fileName); 
    ObjectInputStream objectInputFile = new ObjectInputStream(inStream); 

    //need to create CarOwner[] object called temp and return 
} 

readListFromBinary()方法从二进制文件(ltStateBinary.dat)读取一个ArrayList集合。然后,每个ArrayList对象项然后被写入一个新创建的名为temp的CarOwner []。 temp会返回到调用方法。

编辑:

public CarOwner[] readListFromBinary() throws Exception 
{ 
    String fileName = "ItStateBinary.dat"; 
    FileInputStream inStream = new FileInputStream(fileName); 
    ObjectInputStream objectInputFile = new ObjectInputStream(inStream); 

    ArrayList<CarOwner> read = (ArrayList<CarOwner>)objectInputFile.readObject(); 

    CarOwner[] temp = read.toArray(new CarOwner[read.size()]); 
    return temp; 
} 

没有任何人知道什么是错用这种方法?它给了我编译器警告

回答

0

我不知道你问什么,但假设你写你的ArrayList的文件中像 说:

ArrayList<CarOwner> al = new ArrayList<CarOwner>(); 
FileOutputStream fos = new FileOutputStream("ItStateBinary.dat"); 
ObjectOutputStream oos = new ObjectOutputStream(fos); 
oos.writeObject(al); 

这样的话,你可以阅读的方式,你的已经在这样做:

FileInputStream fis = new FileInputStream("ItStateBinary.dat"); 
ObjectInputStream ois = new ObjectInputStream(fis); 
ArrayList<CarOwner> read = (ArrayList<CarOwner>)ois.readObject(); 

然后就回到你从ArrayList中返回数组:

return read.toArray(new CarOwner[read.size()]);