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

  • Home
  • SEARCH
  • 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 322451
In Process

The Archive Base Latest Questions

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

I have several classes that need to load some properties files, and I was

  • 0

I have several classes that need to load some properties files, and I was wondering what are the best practices to accomplish this. I thought of two basic approaches:

  • Hardcode the properties file name in each class, and then use the Properties class to load from a FileInputStream, which can be problematic if someone decides to change the name of the properties file, as it is hardcoded in the code.

    public class A {
        public A() {
            Properties p = new Properties().load(
                    new FileInputStream("properties/Aconfig.properties"));
            String value = p.getProperty("key", "");
        }        
    }
    
  • Create a method that, given a class name, loads a properties file that has the same name as the class. Though this approach doesn’t require to hardcode the properties file name, it does require that we follow some convention in naming the files, which can cause confusion.

    public class A {
        public A() {
            Properties p = PropertiesHelper.loadFromClassName(A.class.getName());
            // here, we **assume** that there is a A.properties file in the classpath.
        }
    }
    

However, there may be many other more elegant approaches, and that’s why I posed these questions: i) What are the best practices for loading properties files in Java?; ii) Do you use any helper class that takes care of the job?; iii) Where (in the code) do you typically load the properties file?

Also, is it OK for a class to “auto-load” its properties? Or should I pass the arguments that I need to the constructor ? The problem of passing the arguments, is that there are way too many for some classes (~20, which represent parameters in a statistical model).

  • 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-12T08:56:25+00:00Added an answer on May 12, 2026 at 8:56 am

    The best practice would be to use some kind of dependency injection container – for example Spring Framework or Google Guice.

    It solves all kinds of problems – you don’t tie your class to particular property file name, you don’t have to worry about loading said properties within that class and handle possible exceptions, and you don’t have to pass 30 property file names as command line arguments (or system properties).

    It also helps with many, many other things in addition to property loading 🙂 – try it, and you’ll wonder how you ever lived without.

    Update: Incidentally, your other question is also one of those things that Spring would take care of.

    Update #2: I just realized I’ve already tried to push Spring on you 🙂 At the risk of repeating myself, Spring would really help with abstraction here. Depending on what you store in your property files you may not need them at all (if they deal with configuration) or you will get a very nice API for dealing with them (if they’re resource bundles or something similar) .

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

Sidebar

Ask A Question

Stats

  • Questions 245k
  • Answers 245k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer f = open('fname') header = [next(f) for _ in range(header_len)]… May 13, 2026 at 8:09 am
  • Editorial Team
    Editorial Team added an answer I found the issue 'aspnet_WebEvent_LogEvent' expects parameter '@ApplicationPath', which was… May 13, 2026 at 8:09 am
  • Editorial Team
    Editorial Team added an answer This is a duplicate of Mac OS X: Can one… May 13, 2026 at 8:09 am

Related Questions

I need some pretty approach to load polymorhic object I have base class and
Curious if anyone has opinions on which method would be better suited for asp.net
I'm an ICT student, I've gotten classes on several subjects: coding(C#, Java), webdevelopment(XHTML, CSS,
I have written a program which analyzes a project's source code and reports various

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.