文件在项目中的位置:
文件内容:
#图片服务器的地址
IMAGE_SERVER_URL=http://192.168.25.133/
#注:Java代码均在src/test中
- 方法一
/*
* 静态代码块加载:
*/
public class TelellController {
private static String enc_dec_key;//定义变量
static {
Properties pro = new Properties();//新建Properties
try {
//加载
pro.load(TelellController.class.getResourceAsStream("/resource/resource.properties"));
enc_dec_key = pro.getProperty("ENC_DEC_KEY");//获取值
} catch (IOException e) {
e.printStackTrace();
}
}
//使用:获得用户真实手机号;
String realTel =telellService.getRealTel(id,enc_dec_key);
}
//----------------------------------------------------//
InputStream input= null;
static {
Properties pro = new Properties();//新建Properties
try {
//加载
pro.load(input = TelellController.class.getResourceAsStream("/resource/resource.properties"));
enc_dec_key = pro.getProperty("ENC_DEC_KEY");//获取值
} catch (IOException e) {
e.printStackTrace();
}finally{
IOUtils.closeQuietly(input);
}
}
- 方法二
/*
* 这样有个问题,this不能在static(静态)方法或者static块中使用的,
* 原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,
* 而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的
*/
InputStream insss =this.getClass().getResourceAsStream("/conf/resources.properties");
Properties pss = new Properties();
pss.load(insss);
System.out.println(pss.getProperty("IMAGE_SERVER_URL"));
//----------------------推荐--------------------------------//
InputStream insss =Object.class.getResourceAsStream("/conf/resources.properties");
Properties pss = new Properties();
pss.load(insss);
System.out.println(pss.getProperty("IMAGE_SERVER_URL"));
- 方法三
InputStream in = ClassLoader.getSystemResourceAsStream("com/test/modul/utils/prop.properties");
Properties p = new Properties();
p.load(in);
String path = p.getProperty("path");
- 方法四
InputStream in = new BufferedInputStream(new FileInputStream(basePath));
ResourceBundle rb = new PropertyResourceBundle(in);
String path = rb.getString("path");
- 方法五
InputStream in = new BufferedInputStream(new FileInputStream(
new File(basePath)));
Properties prop = new Properties();
prop.load(in);
String path = prop.getProperty("path");
- 方法六
InputStream in = LoadPropertiesFileUtil.class.getResourceAsStream("/com/test/modul/utils/prop.properties");
Properties p = new Properties();
p.load(in);
Strign path = p.getProperty("path");
- 方法七
Resource resource = new ClassPathResource("/conf/resources.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
System.out.println(props.getProperty("IMAGE_SERVER_URL"));
- 方法八
ResourceBundle rb = ResourceBundle.getBundle("com/test/modul/utils/prop");
String path = rb.getString("path");
- 方法九
InputStream in = LoadPropertiesFileUtil.class.getClassLoader().getResourceAsStream("com/test/modul/utils/prop.properties");
Properties p = new Properties();
p.load(in);
String path = p.getProperty("path");
- 完整 Util
/**
* @Description 属性文件工具类
* @author wh
* @date 2016年3月27日
* @version 1.0.0
*/
public class PropertiesUtil {
/**
* @param path 文件路径
* @param key key值
* @return value
*/
public static String getValue(String path, String key) throws IOException{
Properties properties = PropertiesUtil.getProperties(path);
return properties.getProperty(key);
}
/**
* @param path 文件路径
* @param key key值
* @return properties
*/
private static Properties getProperties(String path) throws IOException{
Properties properties = new Properties();
InputStream inputStream = null;
if(StringUtils.isBlank(path)){
//默认加载文件
inputStream = PropertiesUtil.class.getResourceAsStream("/byit.properties");
}else{
inputStream = PropertiesUtil.class.getResourceAsStream(path);
}
properties.load(inputStream);
inputStream.close();
return properties;
}
}
/**
* 读取properties文件
* @author wh
*/
public class PropertiesUtil {
/**
* 方法描述:读取含有字母数字的配置文件
* (不能读取含有中文的内容否则乱码,中文内容请用readChineseProperties方法)
* @param name
* @return properties
*/
public static Properties read(String name){
InputStream in =Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
Properties p = new Properties();
try {
p.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
/**
* 方法描述:读取带有中文内容的配置文件 解决中文乱码
* @param name
* @return properties
*/
public static Properties readChineseProperties(String name){
InputStream in =Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
Properties p = new Properties();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(in,"utf-8"));
p.load(bf);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
}