DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Improving java.util.Properties

Improving java.util.Properties

Zemian Deng user avatar by
Zemian Deng
·
Sep. 15, 12 · Interview
Like (0)
Save
Tweet
Share
9.33K Views

Join the DZone community and get the full member experience.

Join For Free

The Java built-in java.util.Properties class could really use some love. I have written a slightly improved version called timemachine.scheduler.support.Props, and below are some features that I use often.

You can use it as a "String Map" of properties

Props props1 = new Props();
props1.put("foo", "bar");

// It can load from/to the Java Properties
Props props2 = new Props(System.getProperties());
java.util.Properties javaProps = props3.toProperties();

// It can load from/to a basic java.util.Map
Props props3 = new Props(System.getenv());

// Props is a HashMap<String, String>, so no need to convert. Just use it
for(Map.Entry<String, String> entry : props3.entrySet())
    System.our.println(entry.getKey() + ": " + entry.getValue());

You can load from a file in a single line

Props props1 = new Props("config.properties");
Props props2 = new Props("/path/to/config.properties");
Props props3 = new Props(new java.net.URL("http://myhost/config/config.properties"));
Props props4 = new Props(ClasspathURLStreamHandler.createURL("classpath://config/config.properties"));

// You can re-load on top of existing instance to override values
props4.load("config2.properties");

NOTE: The ClasspathURLStreamHandler is a utility class from the same package under timemachine.scheduler.support that can load any resources that's in the classpath.

You can get many basic types conversion

Props props = new Props();
props.put("str", "foo");
props.put("num", "123");
props.put("dec", "99.99");
props.put("flag", "true");

String str = props.getString("str");
int num = props.getInt("num");
double dec = props.getDouble("dec");
boolean flag = props.get("flag");

// You can even get default value when key is not found too
int num2 = props.getInt("num2", -1);

You can auto expand ${variable} from any existing properties

Props props = new Props(System.getProperties());
props.put("configDir", "${user.home}/myapp/config");
props.expandVariables();

// The ${user.home} should be expanded to actual user home dir value.
File dir = new File(props.get("configDir"));

There you have it. You see more code than words in this post, but I believe simple code speak louder than words and docs. I find these features very convenient and practical for many Java applications to use. I wish the JDK would provide these out of the box, and make the java.util.Properties more developer friendly.

Java (programming language) Property (programming) application Java Development Kit dev POST (HTTP) Classpath (Java) Doc (computing)

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Bye-Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: