java4种方式读取配置文件+修改配置文件
java4种⽅式读取配置⽂件+修改配置⽂件
⽅式⼀:采⽤ServletContext读取,读取配置⽂件的realpath,然后通过⽂件流读取出来。
因为是⽤ServletContext读取⽂件路径,所以配置⽂件可以放⼊在web-info的classes⽬录中,也可以在应⽤层级及web-info的⽬录中。⽂件存放位置具体
2022年国庆放假安排时间表
在eclipse⼯程中的表现是:可以放在src下⾯,也可放在web-info及webroot下⾯等。因为是读取出路径后,⽤⽂件流进⾏读取的,所以可以读取任意的配置⽂件包括xml和properties。缺点:不能在servlet外⾯应⽤读取配置信息。
具体举例如下:
//RealPath(name)读取路径
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //读取WEB-INF中的配置⽂件
//所以后⾯的path只需要以应⽤demo/开头具体的部署⽬录路径即可,如上⾯的/web-in…
System.out.println(realPath);
InputStreamReader reader =new InputStreamReader(new FileInputStream(realPath),"utf-8");
Properties props = new Properties();
props.load(reader); //load个⼈建议还是⽤Reader来读,因为reader体系中有个InputStreamReader可以指定编码
String jdbcConValue = Property("jdbc_con");
System.out.println(jdbcConValue);
System.out.println("加载src包下的资源------------------------");
path = "/WEB-INF/classes/com/test/servlet/jdbc_connection.properties"; //读取WEB-INF中的配置⽂件
realPath=getServletContext().getRealPath(path);
System.out.println(realPath);
reader = new InputStreamReader(new FileInputStream(realPath),"utf-8");
props.load(reader); //load个⼈建议还是⽤Reader来读,因为reader体系中有个InputStreamReader可以指定编码
jdbcConValue = Property("jdbc_con");
System.out.println("second::"+jdbcConValue);
}
⽅式⼆:采⽤ResourceBundle类读取配置信息,
优点是:可以以完全限定类名的⽅式加载资源后,直接的读取出来,且可以在⾮Web应⽤中读取资源⽂件。
缺点:只能加载类classes下⾯的资源⽂件且只能读取.properties⽂件。
/**
小狗的小房子故事
* 获取指定配置⽂件中所以的数据
* @param propertyName
*        调⽤⽅式:
*            1.配置⽂件放在resource源包下,不⽤加后缀
*            AllMessage("message");
*            2.放在包⾥⾯的
*              AllMessage("ssage");
* @return
*/
public static List<String> getAllMessage(String propertyName) {
/
口袋妖怪红宝石金手指/ 获得资源包
ResourceBundle rb = im());
// 通过资源包拿到所有的key
Enumeration<String> allKey = rb.getKeys();
// 遍历key 得到 value
List<String> valList = new ArrayList<String>();
while (allKey.hasMoreElements()) {
String key = Element();
String value = (String) rb.getString(key);
valList.add(value);
}
return valList;
}
⽅式三:采⽤ClassLoader⽅式进⾏读取配置信息
优点是:可以在⾮Web应⽤中读取配置资源信息,可以读取任意的资源⽂件信息
缺点:只能加载类classes下⾯的资源⽂件。
/**获取的是class的根路径下的⽂件
* 优点是:可以在⾮Web应⽤中读取配置资源信息,可以读取任意的资源⽂件信息
* 缺点:只能加载类classes下⾯的资源⽂件。
* 如果要加上路径的话:com/test/servlet/jdbc_connection.properties
*/
private static void use_classLoador(){
/
/⽂件在class的根路径
InputStream is=ClassLoader().getResourceAsStream("message.properties");  //获取⽂件的位置
String filePath=ClassLoader().getResource("message.properties").getFile();  System.out.println(filePath);
//获取的是TestJava类所在的相对路径下 ,com/test/servlet/jdbc_connection.properties"
//  InputStream is2=ResourceAsStream("message.propertie");
BufferedReader br= new BufferedReader(new InputStreamReader(is));
Properties props = new Properties();
try {
农行手机转账
props.load(br);
for (Object s : props.keySet())
System.out.println(s);
} catch (IOException e) { e.printStackTrace();}
}
高分子材料与工程专业就业前景
⽅法4 getResouceAsStream
ResourceAsStream 与classloader不同
使⽤的是当前类的相对路径
BufferedReader br=new BufferedReader(
new InputStreamReader(XmlParserHandler.class.
getResourceAsStream("./l"), "GB2312"));// ./代表当前⽬录不写也可以          InputSource is=new InputSource(br);//数据源
中秋送礼祝福语
⽅法5 PropertiesLoaderUtils⼯具类
/**
* Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的⽂件地址加载属性资源  * 最⼤的好处就是:实时加载配置⽂件,修改后⽴即⽣效,不必重启
*/
private static void springUtil(){
Properties props = new Properties();
while(true){
try {
props=PropertiesLoaderUtils.loadAllProperties("message.properties");
for(Object key:props.keySet()){
System.out.print(key+":");
System.out.(key));
}
} catch (IOException e) {
System.out.Message());
}
try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
}
}
修改Properties
/**
* 传递键值对的Map,更新properties⽂件
*
* @param fileName
*            ⽂件名(放在resource源包⽬录下),需要后缀
* @param keyValueMap
*            键值对Map
*/
public static void updateProperties(String fileName,Map<String, String> keyValueMap) {
//getResource⽅法使⽤了utf-8对路径信息进⾏了编码,当路径中存在中⽂和空格时,他会对这些字符进⾏转换,这样,
//得到的往往不是我们想要的真实路径,在此,调⽤了URLDecoder的decode⽅法进⾏解码,以便得到原始的中⽂及空格路径。  String filePath = ClassLoader().getResource(fileName).getFile();
Properties props = null;
BufferedWriter bw = null;
try {
filePath = URLDecoder.decode(filePath,"utf-8");
log.debug("updateProperties propertiesPath:" + filePath);
props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
log.debug("updateProperties old:"+props);
// 写⼊属性⽂件
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
props.clear();// 清空旧的⽂件
for (String key : keyValueMap.keySet())
props.setProperty(key, (key));
log.debug("updateProperties new:"+props);
props.store(bw, "");
} catch (IOException e) {
<(e.getMessage());
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。