读取properties的几种方式

读取properties的几种方式

  • 方法①
public class ResourceUtil {
private static final ResourceBundle bundle = java.util.ResourceBundle
.getBundle("sysConfig");
public static final String getConfigByName(String name) {
String value = "";
try {
value = new String(bundle.getString(name).getBytes("iso8859-1"),
"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}
  • 方法②
public void testPro() throws IOException{
InputStream insss =this.getClass().getResourceAsStream("/conf/resources.properties");
Properties pss = new Properties();
pss.load(insss);
System.out.println(pss.getProperty("IMAGE_SERVER_URL"));
}
  • 方法③:
public void testPro() throws IOException{
InputStream insss =Object.class.getResourceAsStream("/conf/resources.properties");
Properties pss = new Properties();
pss.load(insss);
System.out.println(pss.getProperty("IMAGE_SERVER_URL"));
}
  • 方法④:
public class testProperties extends TestCase{
public void testPro() throws IOException{
Resource resource = new ClassPathResource("/conf/resources.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
System.out.println(props.getProperty("IMAGE_SERVER_URL"));
}
}
  • 方法⑤

xml配置文件中引入的:

        <context:property-placeholder location="classpath:conf/resources.properties" />  




       @Value("${IMAGE_SERVER_URL}")  

       private String IMAGE_SERVER_URL;
  • 方法⑥:
ResourceBundle的方式 ,传入一个inStream
ResourceBundle resource = new PropertyResourceBundle(inStream);
  • 方法⑦:通过文件读取
InputStream inStream = new FileInputStream(new File("filePath"));
  • 方法⑧:在servlet中:
InputStream in = context.getResourceAsStream("filePath");

方法⑨:ulr方式读取

URL url = new URL("path");
InputStream inStream = url.openStream();
  • 方法10:
Properties prop = new Properties();
prop.load(inStream);
String key = prop.getProperty("username");
String key = (String) prop.get("username");
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值