Reading Properties from a File

Reading Properties from a File

It’s not too much trouble to read a few arguments from the command line. It really becomes a problem if you have to read 50 or more arguments. One solution is to prepare a property file and mention all the properties in it. Then you can read the name of the property file from the command line and read the values of all other properties when you need to throughout the application. The syntax of the property file should be in the following format.

 key=value

Keys can be defined in a separate Java class, say PropertyDefiner, and can be implemented as a singleton.

 public static final String CONFIGURATION_FILE = key_in_property_file;

Make use of the java.util.Properties class that represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream.

             //instantiate a new Property class            Properties props = new Properties();// set the name of the property fileif (args[i].equalsIgnoreCase("-f"))props.setProperty(PropertyDefiner.CONFIGURATION_FILE, args[++i]);

Just for the sake of robustness, check if this file is not null while retrieving the file name. Once you have the file name, open a FileInputStream and load the properties. And don’t forget to close the input stream once the job is done.

 	FileInputStream in = new FileInputStream(fileName);	props.load(in);	in.close();

Now you can retrieve the value of any property as you wish.

 String propertyName = props.getProperty(PropertyDefiner.PROPERTY_NAME);

Using this approach, you can add new properties while you are developing the application. You also can fine-tune the performance of your application by merely changing a few values in the property file.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing