Program to Create property files and store key-value pairs in it | And how to read property file in java

In this post >
  • Program 1 to Create property files and store key-value pairs in it in java >
  • Program 2 to read property file in java >



1) Create property files  
2) Create java.util.Properties by using java.util.Properties.setProperty(key, value) method
3) store property in files using FileOutputStream.store(OutputStream, comment) method



Program 1 to Create property files and store key-value pairs in it in java >

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/** JavaMadeSoEasy.com */
public class PropertyFileCreateAndStore {
   public static void main(String[] args) {
          FileOutputStream fos = null;
          try {
                 String propFileName = "c:/MyPropertyFile.properties";
                 // 1) Create property files
                 fos = new FileOutputStream(propFileName);
                 System.out.println(propFileName + " has been created ");
                 // 2) Create java.util.Properties by using
                 // java.util.Properties.setProperty(key, value) method
                 Properties prop = new Properties();
                 prop.setProperty("myName", "Ankit");
                 prop.setProperty("Language", "Java");
                 // 3) store property in files using
                 // FileOutputStream.store(OutputStream, comment) method
                 prop.store(fos, "This is MyPropertyFile");
                 System.out.println("key-value pairs have been stored in "
                              + propFileName);
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fos != null){
                              fos.close(); //close FileOutputStream
                       }
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
  
   }
}
/*OUTPUT
c:/MyPropertyFile.properties has been created
key-value pairs have been stored in c:/MyPropertyFile.properties
*/


c:/MyPropertyFile.properties will look like this >
#This is MyPropertyFile
#Wed Aug 26 18:32:05 IST 2015
Language=Java
myName=Ankit

For more convenience below I have attached screenshot as well.


Program 2 to read property file in java >

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/** JavaMadeSoEasy.com */
public class PropertyFileRead {
public static void main(String[] args) {
     String propFileName = "c:/MyPropertyFile.properties";
     FileInputStream fis = null;
     try {
          // 1) Create FileInputStream
          fis = new FileInputStream(propFileName);
          // 2) Create java.util.Properties and load/read from FileInputStream
          Properties prop = new Properties();
          prop.load(fis);
          System.out.println("Reading from "+ propFileName);
          // 3 ) Read from property file
          System.out.println(prop.getProperty("myName"));
          System.out.println(prop.getProperty("Language"));
     } catch (IOException e) {
          e.printStackTrace();
     } finally {
          try {
              if (fis != null)
                   fis.close(); //close FileInputStream
          } catch (IOException e) {
              e.printStackTrace();
          }
     }
}
}
/*OUTPUT
Reading from c:/MyPropertyFile.properties
Ankit
Java
*/


RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways


Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO


Find current directory or current path in java


No comments:

Post a Comment