Saturday, 10 June 2017

Java Tutorial: Java properties tutorial [How to store key-value pairs in properties file] ~ foundjava


Click here to watch in Youtube :
https://www.youtube.com/watch?v=B-DK57t_qIM&list=UUhwKlOVR041tngjerWxVccw

PropertiesDemo.java
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/*
 * public void store(Writer writer, String comments)
 *                                  throws IOException
 * 
 * Parameters: 
 * ----------
 * 
 * writer - an output character stream writer. 
 * comments - a description of the property list.
 */
class PropertiesDemo
{
    public static void main(String[] args) throws IOException
    {

        try (FileWriter fileWriter = new FileWriter("info.properties"))
        {

            Properties p = new Properties();
            p.setProperty("name", "Ram");
            p.setProperty("email", "ram@yahoo.com");

            /*
             * Writes this property list (key and element
             * pairs) in this Properties table to the output
             * character stream in a format suitable for
             * using the load(Reader) method.
             */
            p.store(fileWriter, "User Info");

            System.out.println("Property file is created successfully..");
        }

    }
}
Output
Property file is created successfully..

info.properties
#User Info
#Wed Mar 01 09:25:09 IST 2017
email=ram@yahoo.com
name=Ram

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment