properties加载的几种方式

文件在项目中的位置:

文件内容:

 #图片服务器的地址  
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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值