java读取properties文件
application.properties配置文件
name=scanio
age=24
解析application.properties代码
package util;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static final String DEFAULT_PROPERTIES="/application.properties";
/**
* 获取properties属性值
*/
public static String getPropValue(String propKey){
try {
Properties props = new Properties();
InputStream inputStream = PropertiesUtil.class.getResourceAsStream(DEFAULT_PROPERTIES);
//*.properties配置文件,要使用UTF-8编码,否则会现中文乱码问题
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
props.load(bf);
return props.getProperty(propKey);
}catch (IOException e){
e.printStackTrace();
}
return null;
}
案例
String name = PropertiesUtil.getPropValue("name");
String age = PropertiesUtil.getPropValue("age");
以上是 java读取properties文件 的全部内容, 来源链接: utcz.com/z/518597.html