1

What am I trying to do

  • Simulate a socialnetwork program,in which you can add your profile,change your status,your picture,add friends etc.The program must save it's content before closing in a file.

How do I intend to do it

  • As I can not append with ObjectOutputStream. I though of creating an ArrayList<SocialProfiles> (SocialProfiles) in this case are the profiles which I am trying to save.
  • I want to load the profiles from the file to the ArrayList when the program starts,and when I am done adding profiles, I want to write the profiles from the ArrayList back to the file.
  • I am using the size of the array as an index.Example when it first writes to the array,it writes to index 0.When it has 1 element it writes to index 1 etc etc.

What is not going as it is supposed to?

  • The program does not load the data from the file to the array.

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();
            }
        }
    }
4
  • Your exception handling is up the pole. Don't write code like this. If you get an exception, don't just log it and then proceed as though it didn't happen. Your program executes in invalid states. Try to use one try block, and as many catch blocks as you need for the various exceptions you're interested in. Commented Jan 2, 2014 at 11:42
  • I am not done with it.Don't worry,I don't just log the messages.And I will make only one try block.This was just a test version.;) Commented Jan 2, 2014 at 12:10
  • It's you and your employer who should be worried. There's no point in doing it badly the first time, or just for test. You aren't testing anything useful by writing incorrect code. Commented Jan 2, 2014 at 21:20
  • @EJP I am a college student,and this is a project that is required by my teacher.I only have 2 months that I am learning Java...I will get better for sure.And buddy you sure are overreacting about using 4 try blocks in the opening. Commented Jan 3, 2014 at 10:12

1 Answer 1

1

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){
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It seems to be working fine now.It didn't cross my mind to write the whole array.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.