Thursday, May 25, 2017

Reading Properties file in java

You can find many ways to read properties file in java, here I am explaining to read properties file using java.io.File and java.util.Properties.

Let say we have below properties file example dataFile.properties which we need to read 







Now we have to read this properties in below manner 


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ReadDataFile {
   public static String USER;
   public static String PASSWORD;
   public static String BROWSER;
   public static String DBURL;
   public static String DBUSER;
   public static String DBPWD;
   public static String DASHBOARDURL;

   public static void ReadPropertyFile() throws IOException {
      File file = new File("dataFile.properties");
      FileInputStream fileInput = null;
      try {
         fileInput = new FileInputStream(file);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      Properties prop = new Properties();
      try {
         prop.load(fileInput);
      } catch (IOException e) {
      }

      USER = prop.getProperty("USER");
      PASSWORD = prop.getProperty("PASSWORD");
      BROWSER = prop.getProperty("BROWSER");
      DBURL=prop.getProperty("DATABASEURL");
      DBUSER=prop.getProperty("DB_USER");
      DBPWD=prop.getProperty("DB_PWD");
      DASHBOARDURL = prop.getProperty("DASHBOARDURL");

   }
}


Here each variable is for values from each property. Hope you like it. Now this variable can be used anywhere in your code where u need this properties values .


0 on: "Reading Properties file in java"