读取 properties

1、 打开文件的方式有:

InputStream is = ResourcePropertiesUtils.class.getClassLoader().getResourceAsStream(name);

   ClassPathResource classPathResource = new ClassPathResource(name);
        try {
            if(classPathResource.exists()){
                is = classPathResource.getInputStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

 FileInputStream fis = null;     
File file = new File(name);
        if(file.exists()){
           
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
}

2、读取配置的方式有

1、

    public static Properties getProperties(InputStream is){
        Properties properties = null;
        try{
            properties = new Properties();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            properties.load(bf);
            is.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return properties;
    }

配置文件读取顺序

private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

代码

public class ResourcePropertiesUtils {

    static Logger log = LoggerFactory.getLogger(ResourcePropertiesUtils.class);

    private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/,";

    public static Properties getProperties(InputStream is){
        Properties properties = null;
        try{
            properties = new Properties();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is));
            properties.load(bf);
            is.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return properties;
    }

    /**
     * 根据文件路径获取流
     * @return
     */

    public static Properties getProperties(String name){
        String[] files = DEFAULT_SEARCH_LOCATIONS.split(",");
        Properties properties = null;
        for (String file:files
             ) {
            if(file!=null){
                String fileName = file.concat(name);
                properties = getStramProperties(fileName);
                if(properties!=null && !CollectionUtils.isEmpty(properties.keySet())){
                    log.info("读取参数:"+fileName);
                }
            }

        }

        return properties;
    }

    public  static Properties getStramProperties(String name){
        InputStream is = ResourcePropertiesUtils.class.getClassLoader().getResourceAsStream(name);
        Properties properties = new Properties();
        if(is!=null){
            log.info("classloader 获取参数:"+name);
            properties = getProperties(is);
        }

        ClassPathResource classPathResource = new ClassPathResource(name);
        try {
            if(classPathResource.exists()){
                is = classPathResource.getInputStream();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        if(is!=null){
            log.info("classpath 获取参数:"+name);
            Properties tmp = getProperties(is);
            if(tmp!=null){
                for (Object key:tmp.keySet()
                     ) {
                    properties.put(key,tmp.get(key));
                }
            }
        }

        File file = new File(name);
        if(file.exists()){
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            if(fis!=null){
                log.info("FileInputStream 获取参数:"+name);
                Properties tmp = getProperties(is);
                if(tmp!=null){
                    for (Object key:tmp.keySet()
                    ) {
                        properties.put(key,tmp.get(key));
                    }
                }
            }
        }


        return properties;
    }


}

测试

@RestController
@RequestMapping("/property")
public class PropertiesController {


    @RequestMapping("/get")
    public Result getProperties(String path){
        Properties properties = ResourcePropertiesUtils.getStramProperties(path);
        Map<Object,Object> map = new HashMap<>();
        for (Object obj: properties.keySet()
             ) {
          map.put(obj,properties.get(obj));
        }
        return Result.success(map);
    }

    @RequestMapping("/getPath")
    public Result getPathProperties(String path){
        Properties properties = ResourcePropertiesUtils.getProperties(path);
        Map<Object,Object> map = new HashMap<>();
        for (Object obj: properties.keySet()
        ) {
            map.put(obj,properties.get(obj));
        }
        return Result.success(map);
    }

}

执行结果

1、debug执行

127.0.0.1:8080/property/getPath?path=application.properties

执行情况 找不到

2、直接运行

目录结构

192.168.128.15:8080/property/get?path=application.properties

192.168.128.15:8080/property/getPath?path=application.properties

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值