我是一個IT學生,是時候完成我的Java最終項目。我遇到了很多問題,這個我無法解決,我非常沮喪。使用對象輸入輸出流與文件和數組列表
我的代碼是這樣的:
在管理類:
public ArrayList cos_info = new ArrayList();
public ArrayList cas_info = new ArrayList();
public int cos_count = 0 ;
public int cas_count = 0 ;
void coustmer_acount() throws FileNotFoundException, IOException{
String add=null;
do{
person p = new person() ;
cos_info.add(cos_count, p);
cos_count ++ ;
add =JOptionPane.showInputDialog("Do you want to add more coustmer..\n'y'foryes ..\n 'n'for No ..");
}
while(add.charAt(0) == 'Y'||add.charAt(0)=='y');
writenew_cos();
// add_acounts();
}
void writenew_cos() throws IOException{
ObjectOutputStream aa = new ObjectOutputStream(new FileOutputStream("coustmer.txt"));
aa.writeObject(cos_info);
JOptionPane.showMessageDialog(null,"Added to file done sucessfuly..");
aa.close();
}
在Coustmer類
:
void read_cos() throws IOException, ClassNotFoundException{
person p1= null ;
int array_count = 0;
ObjectInputStream d = new ObjectInputStream(new FileInputStream("coustmer.txt"));
JOptionPane.showMessageDialog(null,d.available());
for(int i = 0;d.available() == 0;i++){
a.add(array_count,(ArrayList) d.readObject());
array_count++;
JOptionPane.showMessageDialog(null,"Haaaaai :D");
JOptionPane.showMessageDialog(null,array_count);
}
d.close();
JOptionPane.showMessageDialog(null,array_count +"1111");
for(int i = 0 ; i<array_count&& found!= true ; i++){
count ++ ;
p1 =(person)a.get(i);
user=p1.user;
pass = p1.pass;
// cos_checkpass();
}
}
它只是打印JOptionPane.showMessageDialog(null,d.available());
,有一個例外。這裏a.add(array_count,(ArrayList) d.readObject());
p.s:我自己的類中的person對象,它是Serializable。
如果發佈異常的完整堆棧跟蹤,解決方案可能會更快。這將有助於找出問題的根源。 – 2010-05-29 20:02:09
請提供實際的異常(至少是類型和消息) – 2010-05-29 20:04:03
線程「main」中的異常java.io.EOFException at java.io.ObjectInputStream $ BlockDataInputStream.peekByte(ObjectInputStream.java:2552) at java.io.ObjectInputStream .readObject0(ObjectInputStream.java:1297) at shoppingmall.coustmer.read_cos(coustmer.java:68) at shoppingmall.coustmer。java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)(coustmer.java:30) at shoppingmall.shop.log(shop.java:56) at shoppingmall.shop.main(shop.java:29) – 2010-05-29 21:33:14