How to Use the Java Properties API

1) Create the sample.properties file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <entry key="message">Hello World!</entry>
</properties>

2) Create the PropertiesSample.java file:

import java.io.FileInputStream;
import java.util.Properties;

import static java.lang.System.out;

public final class PropertiesSample {

  public static void main(final String[] args) throws Exception {
    final Properties props = new Properties();

    props.loadFromXML(new FileInputStream("sample.properties"));

    out.println(props.getProperty("message"));
  }

}

3) Compile the PropertiesSample.java file.

4) Launch the PropertiesSample class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s