Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:39:27+00:00 2026-06-02T04:39:27+00:00

I want my app to hold data from ArrayList in file in between sessions.

  • 0

I want my app to hold data from ArrayList in file in between sessions. Class I use implements Serializable. When I debug saving seems to go OK, with no exceptions thrown, and going through the loop the right amount of times. Loading loads just some of entries and then throws EOF Exception. The code is here:

public int saveChildren(Context context){
    FileOutputStream fos;
    ObjectOutputStream os;
    try {
        fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
        os = new ObjectOutputStream(fos);
        for(Child c : children){
            os.writeObject(c);
        }
        os.close();
    }
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 1;
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 2;
    }
    return 0;
}

public void loadChildren(Context context){

    FileInputStream fis;
    try {
        fis = context.openFileInput(filename);
        ObjectInputStream is;
        is = new ObjectInputStream(fis);
        while(is.readObject() != null){
            Child c = (Child) is.readObject();
            boolean skip = false;
            for(Child ch: children){
                if(ch.getName().equals(c.getName())){ 
                    skip = true;
                }
                if(ch.getNr().equals(c.getNr())){ 
                    skip = true;
                }
                if(ch.getImei() != null){
                    if(ch.getImei().equals(c.getImei())){
                        skip = true;
                    }
                }
            }
            if(!skip){
                children.add(c);
            }
        }
        is.close();
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (StreamCorruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }catch (OptionalDataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

What causes the errors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:39:30+00:00Added an answer on June 2, 2026 at 4:39 am

    You can directly write the children object into the ObjectOutputStream. ArrayList implements serializable. A second thing you might want to do is flush the stream before closing it with os.flush(). You will have:

    os = new ObjectOutputStream(fos);    
    os.writeObject(children);    
    os.flush();
    os.close();
    

    and for reading:

    is = new ObjectInputStream(fis);
    ArrayList<Child> children = (ArrayList<Child>)is.readObject();
    is.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my iPhone app, I use a sqlite table to hold my data. While
I'm trying to make a custom class to hold data in my iPhone app.
I want to make a class that can hold settings for my app. Its
I want my app to catch downloads of a particular kind of file. Here's
I have an app that when first launched reads in its data from a
i have a test.php file that will hold a facebook app (iframe based php).
I have a web-app-database 3 tier server setup. Web requests data from app, and
I'm writing an Android app that I want to use in conjunction with a
I have a class implementing a data structure storing Comparable objects. Some instances hold
In my Rails app I have a has_many_through relation. I want to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.