Reading Properties File in Java (2024) | TechGeekNext

Reading Properties File in Java (2024)

In this tutorial, we will demonstrate how to read properties files and the steps to configure properties files. To keep the configuration parameters in Java Programming Language properties files are used.

What is Properties File in Java?
Ans:

To hold the configurable parameters of an applications, we mostly use Properties file (properties is an extension of file). Key-value pairs are used to store data in the properties file.

Java - Properties File

  1. The static information is stored in the form of key-value pair in these files. Properties files can also be used for storing strings for localized and internationalized, which is also known as Property Resource Bundles.
  2. Resource Bundles are a complete stack of resources that manages resources using a set of static strings for a locale from a property file.
  3. The advantage of using Properties files is that over a period of time some information has to change, to change the information there is no need of changing in the code, the change is done only in the key-value pair. The flexibility of the configuration is achieved by using properties files. The hard code which cannot be part of java goes in properties.
  4. The extension of the properties files is: .properties

Take a look at our Suggested Posts :

What is a FileInputStream in Java?
Ans:

FileInputStream class (java.io package) helps to read data from a file as a stream of bytes. FileInputStream extends the InputStream (abstract class).

Syntax to read the data from file

// File path in string format
FileInputStream input = new FileInputStream(filePath);

OR

// file object which will have file path
FileInputStream input = new FileInputStream(File fileObj);

Steps for Reading Properties file with properties.load:

  1. Create Java project.
  2. Create credentials.properties file (New->File) under resources folder.
  3. Provide sample data in key value format as given below:
    Key=name, value=raj,
    key=age,  value=29,
    key=course, value=selenium,
    key=salary, value=20000
  4. Create Java class name ReadPropertiesFileTest.java.
    import java.io.*;
    import java.util.*;
    public class ReadPropertiesFileTest {
    
      public static Properties readPropertiesFile(String fileName) throws IOException {
        FileInputStream fis = null;
        Properties prop = null;
        try {
    	 //FileInputStream class helps to read data from a file as a stream of bytes.
          fis = new FileInputStream(fileName);
          prop = new Properties();
    	  // load the properties file
          prop.load(fis);
        } catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
        } catch (IOException ioe) {
          ioe.printStackTrace();
        } finally {
          fis.close();
        }
        return prop;
      }
    
    
      public static void main(String args[]) throws IOException {
        Properties prop = readPropertiesFile("credentials.properties");
    
    	// read the values form properties file by using the keys
        System.out.println("username: " + prop.getProperty("username"));
        System.out.println("password: " + prop.getProperty("age"));
      }
    
    }
    
    
    
    Output

    username: raj
    age: 29
    

Recommendation for Top Popular Post :