使用对象输入\\输出流文件和数组列表 [英] using Object input\ output Streams with files and array list

查看:122
本文介绍了使用对象输入\\输出流文件和数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个IT的学生,它的时间来完成我在Java中最后一个项目。我所面临的问题很多,这一次我没能解决,我很沮丧。

I'm an IT student, and it's time to finish my final project in Java. I've faced many problems, this one I couldn't solve and I'm really frustrated.

我的code是这样的:

My code is like this:

在管理类:

 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类:

in Coustmer class:

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:从我自己的类人对象和它的序列化。

It just prints JOptionPane.showMessageDialog(null,d.available() ); and has an exception. Here a.add(array_count,(ArrayList) d.readObject()); p.s : person object from my own class and it's Serializable.

推荐答案

我建议把一个try / catch周围抛出异常的抓住你的方法调用

I would suggest putting a try/catch around your method call that throws the exception in the catch

catch(Exception ex){
ex.printStackTrace();
}

要找出错误是从正好来了,它可能给你的更多信息,你会得到什么。

to find out where the error is coming from exactly, it might give you more information that what you're getting.

另外,我会建议使用

JOptionPane.showMessageDialog(null, "Add more Customers?", "Continue?",JOptionPane.ERROR_MESSAGE);

之类的东西,而不是让用户键入的内容的字符,它创建了一个更友好的用户界面。

or something like that rather than getting the character of what the user typed, it creates a more user friendly interface.

这篇关于使用对象输入\\输出流文件和数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆