java 读取properties的方法,源码

本文介绍了一个简单的Java程序示例,展示了如何通过类路径加载属性文件并从中获取特定属性值的方法。此示例使用了标准的Java Properties类来处理配置文件。

首先 ,类开头写上 

private Properties properties = new Properties();

 

主方法里 这样写   String adds = getProperty("adds");

方法实现

public String getProperty(String name) {
  String adds = null;
  InputStream in = this.getClass().getResourceAsStream(
    "acpInterface.properties");
  if (in == null) {
   throw new RuntimeException("acpInterface.properties not found");
  }
  if (!(in instanceof BufferedInputStream))
   in = new BufferedInputStream(in);
  try {
   properties.load(in);
   adds = properties.getProperty(name);
   in.close();
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException(
     "Error while processing acpInterface.properties", e);
  }
  return adds;
 }

 

 

文件和 类放在同一包下面

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值