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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:13:43+00:00 2026-06-13T00:13:43+00:00

Short way to reproduce the problem in my real project. Environment: Android SDK 1.16,

  • 0

Short way to reproduce the problem in my real project. Environment: Android SDK 1.16, Eclipse 4.2.0, Windows. Create default Android application and add the following code to MainActivity.java:

private void Save1(boolean externalStorage)
{
    String s = "12345";
    File file;
    FileOutputStream fos = null;

    if ( externalStorage )
    {
        try
        {
            file = new File(getExternalFilesDir(null), "log");
            fos = new FileOutputStream(file);                  // Resource leak: 'fos' is never closed
        }
        catch(FileNotFoundException e)
        {
            return;
        }
    }
    else
    {
        try
        {
            fos = openFileOutput("log", Context.MODE_PRIVATE);
        }
        catch(FileNotFoundException e)
        {
            return;
        }
    }

    try
    {
        fos.write(s.getBytes());
        fos.close();
    }
    catch(IOException e)
    {
        return;
    }
}

private void Save2(boolean externalStorage)
{
    String s = "12345";
    File file;
    FileOutputStream fos = null;

    try
    {
        file = new File(getExternalFilesDir(null), "log");
        fos = new FileOutputStream(file);                  // OK
    }
    catch(FileNotFoundException e)
    {
        return;
    }

    try
    {
        fos.write(s.getBytes());
        fos.close();
    }
    catch(IOException e)
    {
        return;
    }
}

Line fos = new FileOutputStream(file) in Save1 function, warning: Resource leak: 'fos' is never closed

The same line in Save2 function: no warnings.

Please don’t send untested answers, the problem is not so simple as it looks. Adding fos.close() to different parts of the function doesn’t help.

  • 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-13T00:13:43+00:00Added an answer on June 13, 2026 at 12:13 am

    It also go away if i add an finally block to the try in the if block like this:

    if (externalStorage) {
                try {
                    fos = new FileOutputStream(new File(getExternalFilesDir(null),
                            "log"));
                } catch (FileNotFoundException e) {
                    return;
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    fos = openFileOutput("log", Context.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    return;
                }
            }
    

    It is getting interesting…

    So my guess would be, So if you are opening an Stream in a try block and catch block has an return statement then there should be a finally block that close the stream.

    Something like that..

    A tried the same code in a simple java project in eclipse and still got the warning. So it looks like is not related to lint or android. It looks like the eclipse compiler issue. Following is the code, I had to create a dummy openFileOutput() method since it is not available n java:

    private void Save1(boolean externalStorage) {
        String s = "12345";
        FileOutputStream fos = null;
    
        if (externalStorage) {
            try {
                fos = new FileOutputStream(new File("c://", "log"));
            } catch (FileNotFoundException e) {
                return;
            }
        } else {
            try {
                fos = openFileOutput("log", -1);
            } catch (FileNotFoundException e) {
                return;
            }
        }
    
        try {
            fos.write(s.getBytes());
            fos.close();
        } catch (IOException e) {
            return;
        }
    }
    
    /**
     * @param string
     * @param i
     * @return
     */
    private FileOutputStream openFileOutput(String string, int i)
            throws FileNotFoundException {
            return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a short way to create the getter and setter in c#? public
If there any short way besides foreach loop and new collection creation to add
Possible Duplicate: Short way to create arrays? I can create an ArrayList, but, is
Is there a short way to add List<> to List<> instead of looping in
Is there a short way to do this? def value @val end def value=(value)
Is there a short way to check/get for least significant bit in a 32-bit
Is there any way to do things the short and easy way like below?
Short: Is there a way we can push to multiple remotes simultaneously using Github
Short version: I'm trying to determine the best way to track what the user
Short Version: Is there a/what is the suggested way to return error details to

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.