码迷,mamicode.com
首页 > 其他好文 > 详细

读取properties的几种方式

时间:2021-02-02 10:37:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:hold   ati   nss   ace   引入   pre   配置   gets   sys   

读取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");

读取properties的几种方式

标签:hold   ati   nss   ace   引入   pre   配置   gets   sys   

原文地址:https://www.cnblogs.com/lan-syy/p/14354613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!