property配置文件读取工具类

//property配置文件属性获取工具类
public class PropertyReadUtil{

private static PropertyReadUtil instance;
//公用的构造方法
private PropertyReadUtil(){}
//单例Singleton
public static PropertyReadyUtil getInstance(){
  if(instance == null){
             instance = new PropertyReadUtil();
    }
    return  instance;
}

public String getUrl(String key){
  //获取文件路径
  InputStrean input = getClass().getResourceAsStream("xxx.property");
  Properties pro = new Properties();
  try{
     //加载property文件
     pro.load(input);
     //返回property文件指定标识
     return (String)pro.get(key);
  }catch(IOException e){
      e.printStackTrance();
    }
  return null;
   }

public static void main(String [] args){
     PropertyReadUtil pru = new PropertyReadUtil.getInstance();
     String myName = pru.getUrl("myName");
     //输出的内容为test
     System.out.println(myName);
   }

}

2.property配置文件已xxx.properties为后缀命名。
  myName=test

本例使用的是Singleton对property配置文件进行读取的,在使用的过程中只能被实例化一次。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值