0

UPDATE: After a bit of testing I've determined the file itself isnt being created, at least according to the file.exists check anyway. Any ideas?

Hi I'm trying to serialize an arraylist when my app is exited and read it back when its resumed. It doesnt seem to be creating the file.

Here is my code.

protected void onPause() {
    super.onPause();

    if(!myArrayList.isEmpty())
    {
        final String FILENAME = "myfile.bin";
        try{
            FileOutputStream fos;

            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

            //FileOutputStream fileStream = new FileOutputStream(FILENAME);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(myArrayList);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
    }
}

@SuppressWarnings("unchecked")
    @Override
    protected void onResume() {
        super.onResume();

    File file = new File("myfile.bin");
    if(file.exists()){
        final String FILENAME="myfile.bin";
        try{
            FileInputStream fileStream = new FileInputStream(FILENAME);
            ObjectInputStream os = new ObjectInputStream(fileStream);
            myArrayList = (ArrayList<MyObject>)os.readObject();
            os.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
        }
    }




}

Any ideas? My MyObject class implements serializable.

8
  • The e.getMessage() in your catch statements does absolutely nothing so you are essentially silently ignoring any thrown exceptions. Improving your error handling will help you pinpoint the issue. Commented Mar 23, 2011 at 21:47
  • @Chris Knight Added some more handling filenotfound and ioexcpetion still no errors in logcat/error log. Commented Mar 23, 2011 at 21:51
  • Can you update the above code with your new error handling? Commented Mar 23, 2011 at 21:55
  • Aside: you should definitely consider moving the IO you are doing into Loaders or AsyncTasks Commented Mar 23, 2011 at 22:06
  • I've never developed Andriod before, but can you not put System.out.println statements after each step in the code above to verify what is (or is not) happening? Commented Mar 23, 2011 at 22:09

2 Answers 2

1

Got it to work by changing FileInputStream fileStream = new FileInputStream(FILENAME) to FileInputStream fileStream= openFileInput(FILENAME).

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to see you solved your problem! Don't forget to mark your answer as accepted by clicking the check mark at left.
0
os.writeObject(myArrayList);
os.flush();
os.close();

Try os.close() immediately after writeObject, it should call flush() anyways.

3 Comments

That was my original code, I added the flush after seeing it as a suggestion somewhere on google. Still didn't work.
confirm if MyObject is in the classpath, see if simple ArrayList<String> works.
The class is in the same package/directory of the main program if thats what you mean?

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.