properties 文件操作

                    properties  文件操作


一、路径获取:

(1)绝对路径的获取        

File systemPropertiesFile=new File("/Users/liushuaic/git/yikangPortal2/yikangportal/
src/main/webapp/WEB-INF/classes/system.properties");
FileInputStream fileInputStream;
try {
    fileInputStream = new FileInputStream(systemPropertiesFile);
    systemProperties.load(fileInputStream);
    systemProperties.getProperty("invitationUrl");
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

     (2)获取当前类路径    

String currentPath=SystemProperties.class.getResource(".").getPath();
 输出:/Users/liushuaic/git/yikangPortal2/yikangportal/src/main/webapp/WEB-INF/classes/com/yikang/base/utils/   当前的类路径

    

(3)通过相对路径获取   

String filePath=SystemProperties.class.getResource("/system.properties").getPath();
输出:/Users/liushuaic/git/yikangPortal2/yikangportal/src/main/webapp/WEB-INF/classes/system.properties   得到了 WEB-INF/classess 下的system.properties文件


二、加载properties 文件

// 获取WEB-INF 下的  system.properties 文件

InputStream  inputStream=SystemProperties.class.getResource("/system.properties").openStream();
systemProperties.load(inputStream);
System.out.println(systemProperties.getProperty("invitationUrl"));


/**
 * @author liushuaic
 * @date 2015/12/10 11:55
 * @desc system配置信息
 * **/
public class SystemProperties {
    private static Properties systemProperties=new Properties();
    static {
      try {
            InputStream  inputStream=SystemProperties.class.getResource("/system.properties").openStream();
        systemProperties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
}
/**
 * @author liushuaic
 * @date 2015/12/01 11:54
 * @desc 获取systemProperties 文件中的值
 * */
    public String getPropertieValue(String key){
        return systemProperties.getProperty(key);
    }
}


转载于:https://my.oschina.net/lavedream/blog/541925

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值