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 6222419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:18:38+00:00 2026-05-24T08:18:38+00:00

I’ve got (an admittedly strange) try/catch block that checks if a File exists. If

  • 0

I’ve got (an admittedly strange) try/catch block that checks if a File exists. If it doesn’t, then I create it and write a File to it. Then I read the File.

I have an ArrayList that is being initialized by reading an object from an ObjectInputStream. In the original writing of the file using a FileOutputStream and an ObjectOutputStream, if I call close() on them, then my ArrayList remains null. If I simply remove the calls to close(), everything goes as planned.

This seems backwards to me. I would think that the file system is somehow “locked” if I’ve got some type of stream open to it. That’s why I thought to close the outputStreams before using an inputStream. But no, Java won’t let this work unless I keep the outputStreams open.

Here’s my code, including the commented out calls to close() which allows this to work properly:

try {
                File file = context.getFileStreamPath("file");    
            } catch (Throwable e) {
                e.printStackTrace();

                try {
                FileOutputStream fileOutputStream = context.openFileOutput("file", Context.MODE_PRIVATE);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

                reminders = new ArrayList<Reminder>();
                objectOutputStream.writeObject(reminders);
//                fileOutputStream.close();
//                objectOutputStream.close();
                } catch (Throwable t) {
                     t.printStackTrace();
                }
            }

         try {
            FileInputStream fileInputStream = context.openFileInput("file");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            if (objectInputStream != null) {
                reminders = (ArrayList<Reminder>) objectInputStream.readObject();
            }
        }  catch (Throwable t) {
                     t.printStackTrace();
        }
  • 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-05-24T08:18:40+00:00Added an answer on May 24, 2026 at 8:18 am

    You should not close the fileOutputStream. The close for objectOutputStream will close the fileOutputStream.

    Closing the fileOutputStream beafore the objectOutputStream will prevent the objectObjectStream from flushing it’s contents to the file, since the fileOutputStream has already been closed.

    Not sure why not closing either is allowing the code to work, unless the the objectOutputStream finalize method is flushing the contents when it goes out of scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.