properties加载的几种方式

简介: 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;
    }
}


相关文章
|
5月前
|
存储 自然语言处理 Java
ResourceBundle.getBundle()来读取自定义的properties配置文件
ResourceBundle.getBundle()来读取自定义的properties配置文件
132 1
|
10天前
|
消息中间件 NoSQL 安全
(转)Spring Boot加载 不同位置的 application.properties配置文件顺序规则
这篇文章介绍了Spring Boot加载配置文件的顺序规则,包括不同位置的application.properties文件的加载优先级,以及如何通过命令行参数或环境变量来指定配置文件的名称和位置。
|
5月前
|
前端开发 Java Spring
properties配置文件的读取
properties配置文件的读取
|
JSON Java 数据格式
【SpringBoot】配置文件的加载与属性值的绑定
【SpringBoot】配置文件的加载与属性值的绑定
【SpringBoot】配置文件的加载与属性值的绑定
Kettle配置数据源时加载外部properties配置文件
Kettle配置数据源时加载外部properties配置文件
|
Java Spring
SSM 项目因为需要加载多个properties配置文件,处理方式
SSM 项目因为需要加载多个properties配置文件,处理方式
235 0
|
Java 测试技术 Apache
实战小技巧16:Properties配置文件自动装载JavaBean
SpringBoot的配置自动装载,使用起来还是很舒爽的,可以非常简单的将properties配置文件的内容,填充到Java bean对象中,如果我们现在是一个脱离于Springboot框架的项目,想实现上面这个功能,可以怎么来做呢?
352 0
|
存储 编解码 Java
Properties 属性操作|学习笔记
快速学习 Properties 属性操作
150 0
Properties 属性操作|学习笔记
Properties 文件的编写与读取
SpringBoot 中的两大配置 properties yaml
2137 0
|
存储 Java
JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现一个文件管理器,简单的,但是在此之前,我们先来做点小案例 package com.
1261 0