Java used Properties class to store above key values pair. Properties.load method is very convenient to load properties file in form of key values pairs.
Properties file looks something like this.
There are two ways you can do it.
- Read properties file from system
- Read properties file from classpath
Read properties file from System :
java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package org.arpit.java2blog; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; /* * @author Arpit Mandliya */ public class ReadPropertiesFileJavaMain { public static void main(String args[]) throws IOException { System.out.println("Reading from properties file"); System.out.println("-----------------------------"); Properties prop=readPropertiesFile("config.properties"); System.out.println("host : "+prop.getProperty("host")); System.out.println("username : "+prop.getProperty("username")); System.out.println("password : "+prop.getProperty("password")); System.out.println("-----------------------------"); } public static Properties readPropertiesFile(String fileName) throws IOException { FileInputStream fis=null; Properties prop=null; try { fis= new FileInputStream(fileName); // create Properties class object prop=new Properties(); // load properties file into it prop.load(fis); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } finally { fis.close(); } return prop; } } |
When you run above program, you will get following output:
1 2 3 4 5 6 7 8 |
Reading from properties file ----------------------------- host : localhost username : java2blog password : java123 ----------------------------- |
Read properties file from classpath:
You can read properties file to classpath too. you have $project/src as default classpath as this src folder will be copied to classes. You can put it in $project/src folder and read it from there.
you need to use
1 2 3 |
this.getClass().getResourceAsStream("/config.properties"); |
to read it from classpath.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package org.arpit.java2blog; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /* * @author Arpit Mandliya */ public class ReadPropertiesFileJavaMain { public static void main(String args[]) throws IOException { ReadPropertiesFileJavaMain rp=new ReadPropertiesFileJavaMain(); System.out.println("Reading from properties file"); System.out.println("-----------------------------"); Properties prop=rp.readPropertiesFile("/config.properties"); System.out.println("host : "+prop.getProperty("host")); System.out.println("username : "+prop.getProperty("username")); System.out.println("password : "+prop.getProperty("password")); System.out.println("-----------------------------"); } public Properties readPropertiesFile(String fileName) throws IOException { InputStream fis=null; Properties prop=null; try { prop=new Properties(); fis=this.getClass().getResourceAsStream(fileName); // create Properties class object if (fis != null) { // load properties file into it prop.load(fis); } else { throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath"); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } finally { fis.close(); } return prop; } } |
When you run above program, you will get following output:
1 2 3 4 5 6 7 8 |
Reading from properties file ----------------------------- host : localhost username : java2blog password : java123 ----------------------------- |
Join Our News Letter – Stay Updated
Subscribe to Awesome Java Content.