自己写了一个读取配置文件的类 希望对大家有用:
--------------------------------------------------------
package com.woyo.free;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class readPro {
private static final Logger logger = LoggerFactory.getLogger(readPro.class);
private static Properties properties = new Properties();
static {
String fileName = "reader.properties";
InputStream in = readPro.class.getResourceAsStream("/" + fileName);
if (in == null) {
logger.error(fileName + " not found");
throw new RuntimeException(fileName + " not found");
} else {
if (!(in instanceof BufferedInputStream))
in = new BufferedInputStream(in);
try {
properties.load(in);
in.close();
logger.debug("set.properties loaded");
} catch (Exception e) {
logger.error("Error while processing set.properties");
throw new RuntimeException(
"Error while processing set.properties", e);
}
}
}
public static String getProperty(final String key) {
return properties.getProperty(key);
}
public static void setProperty(final String key, final String value) {
properties.setProperty(key, value);
}
/**
*
*/
public static void main(String []args){
String msg = readPro.getProperty("name"); //key
System.out.println(msg);
}
}
分享到:
相关推荐
利用java可以读取配置文件信息,可以流来处理这个问题
JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
最近写一个小程序,需要读取保存在配置文件中的配置信息。 这是一个简单的例子,可以以这个程序为基础写一个完整的类出来。
java读写properties配置文件
NULL 博文链接:https://z-gxjs.iteye.com/blog/1846423
使用时注意:两个文件在同一文件下,代码中有使用的例子,欢迎大家下载
在web项目中读取yml配置文件的工具类.可以实现将 server : port : portnumber : 8081 转换为 key为"server.port.portnumber",值为"8081"的Map,String>集合
使用Java读取XML配置文件
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
java如何读取properties配置文件
动态修改配置文件, 支持中文且乱码问题已修复。配置文件读写更便捷
该资源详细介绍了Java如何读取各种配置文件。如xml ini 等等
java读取、更新ini配置文件,可以分块配置