หน้าเว็บ

วันศุกร์ที่ 21 ธันวาคม พ.ศ. 2555

java read .properties file




properties location
classpath:config.properties
...
...
welcome=Hello Word Java!
...
...
PropertiesFile.java
package com.blogspot.na5cent.jsflearning;

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

/**
 *
 * @author Redcrow
 */
public class PropertiesFile {

    private static Properties properties;

    public static synchronized Properties load(String path) throws IOException {
        if (properties == null) {
            properties = new Properties();
        }
        properties.load(PropertiesFile.class.getResourceAsStream(path));
        return properties;
    }
}

Test.java
package com.blogspot.na5cent.jsflearning;

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

/**
 *
 * @author Redcrow
 */
public class Test {

    public static void main(String[] args) {
        Properties properties;
        try {
            properties = PropertiesFile.load("/config.properties");
            System.out.println(properties.getProperty("welcome"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น