簡體   English   中英

程序在使用ObjectInputStream,ObjectOutputStream時無法正確加載數據

[英]Program does not load data correctly while working with ObjectInputStream,ObjectOutputStream

我想做什么

  • 模擬一個社交網絡程序,您可以在其中添加個人資料,更改狀態,您的圖片,添加朋友等。該程序必須先保存其內容,然后再關閉文件。

我打算怎么做

  • 由於我不能追加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();
            }
        }
    }

嘗試序列化/反序列化整個數組,而不是每個對象。

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){
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM