读写propert文件的例子

来源:互联网 发布:所罗门矩阵 编辑:程序博客网 时间:2024/05/18 02:58
import java.io.InputStream;
import java.util.Properties;

public class PropertiesHelper {
    private static final String CONFIG_FILE = "user.properties";
    
    public static void main(String[] args) throws Exception {
        Properties prop = load();
        System.out.println(prop.get("strUserId"));
    }

    private static Properties load(){
        InputStream in = null;
        Properties properties = null;
        try {
            properties = new Properties();
            in = PropertiesHelper.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
            properties.load(in);
            Properties localProperties1 = properties;
            return localProperties1;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }    
        }
        return properties;
    }
    
    public static String getProperties(String key){
        Properties prop = load();//每次都重新加载
        if(prop != null){
            System.out.println("key:"+key);
            return (String)prop.get(key);
        }
        
        return null;
    }
    
}
0 0
原创粉丝点击