All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Read Properties File In Java

Last Updated : Mar 11, 2024

Read Properties File In Java

In this article we will show you the solution of read properties file in java, the properties object that contains a string key and value pair. The java.util.Properties category is a Hashtable subclass. It has the ability to determine the value of a property based on its key.

The Properties class includes steps for reading data from and writing data to the properties file. It can also be used to obtain system properties.

If the information in a properties file is changed, no recompilation is required: If any information in the properties file is changed, you are not required to recompile the java class.

It is employed to store information that is frequently changed.

Let's look at the property files:

  • The properties file contains everything in key-value pairs.
  • Therefore, we may retrieve any value in the this file using the key.

The characters are written as the contents of the file. Four types of exceptions can be thrown, and it returns the file path. Files with short content are better suited for this method.

Path is another class used to link the filename to the path where the content will be stored.

The Files class also has a method called readString() which reads the content of any existing file and is used in the code to check if the content has been written correctly.

A constant set of properties is represented by the Hashtable class subclass known as The Properties. You can load the Properties from a stream or store them to one.

In the property list, each key and its matching value are strings. Java allows you to externalise configuration and save key-value pairs in the Properties file.

The Properties class's Properties.load() function makes it simple to load.properties files that contain key-value pairs.

Step By Step Guide On Read Properties File In Java :-

import java.util.*;
import java.io.*;
public class TalkersCode{
public static void main(String[] args)throw exception{
   Reader = new File("db.propertie");
    Properties p=new Properties();
    p.load(reader);
    System.out.println(p.getProperty("user"));
    System.out.println(p.getProperty("password"));
}
}
  1. First, we start creating an import method called java.io.*
  2. The following step involves creating a class named TalkersCode read property file.
  3. As a result, as a string argument, a public static void main is created.
  4. After which we throw an exception during the program's creation.
  5. Use a file reader to read the file's content.
  6. We then assign properties.
  7. After that, we load the file.
  8. After that, we close the programme with system.out.println.

Conclusion :-

The Properties class includes steps for reading data from and writing data to the properties file.

It can also be used to obtain system properties. If the information in a properties file is changed, no recompilation is required.

I hope this article on read properties file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪