Lind
Lind

Reputation: 243

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

What am I trying to do

How do I intend to do it

What is not going as it is supposed to?

What do I call at 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();
        }
    });

Methods

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();
            }
        }
    }

Upvotes: 1

Views: 87

Answers (1)

user987339
user987339

Reputation: 10707

Try to serialize/deserialize whole array instead of each object.

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

Upvotes: 1

Related Questions