【问题标题】:Program does not load data correctly while working with ObjectInputStream,ObjectOutputStream使用 ObjectInputStream、ObjectOutputStream 时程序无法正确加载数据
【发布时间】:2014-01-02 10:44:58
【问题描述】:

我想做什么

  • 模拟一个社交网络程序,您可以在其中添加您的个人资料、更改您的状态、您的照片、添加朋友等。该程序必须在关闭文件之前保存其内容。

我打算怎么做

  • 因为我无法附加 ObjectOutputStream。我想在这种情况下创建一个ArrayList<SocialProfiles> (SocialProfiles) 是我要保存的配置文件。
  • 我想在程序启动时将配置文件从文件加载到 ArrayList,当我添加完配置文件后,我想将 ArrayList 中的配置文件写回文件。
  • 我使用数组的大小作为索引。例如,当它第一次写入数组时,它会写入索引 0。当它有 1 个元素时,它会写入索引 1 等等。

什么事情没有按预期进行?

  • 程序不会将文件中的数据加载到数组中。

我在 Main 叫什么

try {
        fileoutput = new FileOutputStream("database.dat");
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        output = new ObjectOutputStream(fileoutput);
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        fileinput = new FileInputStream("database.dat");
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        System.out.println("File database.dat nuk ekziston");
    }
        try {
            input = new ObjectInputStream(fileinput);
        } catch (IOException e2) {

        }
loadData();
    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e) 
        {
            new Thread() 
            {
                 @Override
                 public void run() 
                 {
                     writeData();
                     try {
                        fileinput.close();
                         fileoutput.close();
                         input.close();
                         output.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                     System.exit(0);
                 }
            }.start();
        }
    });

方法

public void loadData(){



                try{
                    while (true)
                    {

                    SocialProfile temp; 
                    temp = (SocialProfile)input.readObject();
                    profiles.add(profiles.size(),temp);
                    }
                }
                catch(NullPointerException e){


                }
                catch(EOFException e){
                    System.out.println("U arrit fund-i i file");

                } catch (ClassNotFoundException e) {
                    System.out.println("Objekt-i i lexuar nuk u konvertua dot ne klasen e caktuar");
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


    }

public void writeData(){
        SocialProfile temp;
        for(int i=0;i<profiles.size();i++){
            temp=profiles.get(i);   
            try {
                output.writeObject(temp);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

【问题讨论】:

  • 您的异常处理非常出色。不要写这样的代码。如果您遇到异常,不要只是记录它,然后就好像它没有发生一样继续进行。您的程序在无效状态下执行。尝试使用 一个 try 块,以及你感兴趣的各种异常所需的尽可能多的 catch 块。
  • 我还没做完。别担心,我不只是记录消息。而且我只会做一个尝试块。这只是一个测试版本。;)
  • 应该担心的是您和您的雇主。第一次做得不好或只是为了测试没有意义。编写不正确的代码并没有测试任何有用的东西。
  • @EJP 我是一名大学生,这是我老师要求的一个项目。我只有两个月的时间来学习 Java...我肯定会变得更好。还有哥们你肯定对在开头使用 4 个 try 块反应过度了。

标签: java serialization file-io arraylist


【解决方案1】:

尝试序列化/反序列化整个数组而不是每个对象。

public void serializeData(String filename, ArrayList<SocialProfile>arrayList) {
    FileOutputStream fos;
    try {
        fos = openFileOutput(filename, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(arrayList);
        oos.flush();
        oos.close();
    } catch (FileNotFoundException e) {
        ...
    }catch(IOException e){
        ...
    }
}

private ArrayList<SocialProfile> deserializeData(String filename){
    try{
        FileInputStream fis = openFileInput(filename);
        ObjectInputStream ois = new ObjectInputStream(fis);
        return (ArrayList<SocialProfile>)ois.readObject();
    } catch (FileNotFoundException e) {
        ...
    }catch(IOException e){
        ...
    }catch(ClassNotFoundException e){
        ...
    }
}

【讨论】:

  • 现在似乎工作正常。我没想到要写整个数组。谢谢
猜你喜欢
  • 2017-09-07
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2015-01-05
  • 2014-08-19
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
相关资源
最近更新 更多