Thursday, August 6, 2015

[Java] Read Properties File

Sample Java code that read properties file.

Create properties file.

best-boxer.properties:
name=Manny
lastname=Pacquiao

Create a class that will read properties file.


import java.util.Properties;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;

public class ReadProperties {

public static void main(String[] args) {
   Properties prop = new Properties();
   InputStream inputStream;
   try {
      inputStream
         new FileInputStream("best-boxer.properties");
      prop.load(inputStream);

      System.out.println(">>The best boxer is ::: " +       prop.getProperty("name") + " " 
         +  prop.getProperty("lastname") );

   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
}//End main method



}//End class

Run the class.

>>The best boxer is ::: Manny Pacquiao

No comments:

Post a Comment