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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:06:12+00:00 2026-06-07T18:06:12+00:00

I have following code : // Read properties file. Properties properties = new Properties();

  • 0

I have following code :

    // Read properties file.
     Properties properties = new Properties();
     try {
     properties.load(new FileInputStream("filename.properties"));
     } catch (FileNotFoundException e) {
     system.out.println("FileNotFound");
     }catch (IOException e) {
     system.out.println("IOEXCeption");
     }

Is it required to close the FileInputStream? If yes, how do I do that? I am getting a bad practice error in my code checklist . Asking it to put finally block.

  • 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-07T18:06:14+00:00Added an answer on June 7, 2026 at 6:06 pm

    You must the close the FileInputStream, as the Properties instance will not. From the Properties.load() javadoc:

    The specified stream remains open after this method returns.

    Store the FileInputStream in a separate variable, declared outside of the try and add a finally block that closes the FileInputStream if it was opened:

    Properties properties = new Properties();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream("filename.properties");
        properties.load(fis);
    } catch (FileNotFoundException e) {
        system.out.println("FileNotFound");
    } catch (IOException e) {
        system.out.println("IOEXCeption");
    } finally {
        if (null != fis)
        {
            try
            {
                fis.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    

    Use try-with-resources since Java 7:

    final Properties properties = new Properties();
    try (final FileInputStream fis =
             new FileInputStream("filename.properties"))
    {
        properties.load(fis);
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFound: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("IOEXCeption: " + e.getMessage());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code trying to read a properties file: Properties prop =
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have got the following code from here to read an Excel file using
I have code that basically does the following to read in properties that are
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE
I have the following code: f = open(path, 'r') html = f.read() # no
i have the following code: <p class=more><a href=/stories class=more>Read more</a></p> and CSS: a.more, a.back
Have a look at the following code you are not required to read the
i executed the following c code on my ubuntu machine...i have read about fcntl()'s
I have a list of object properties read from xml file and want 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.