java读取classpath下的文件路径的多种方式

本文分享几种读取classpath(maven项目即resources下面的文件)下properties文件的方式,其他类型的文件也可以转为InputStream使用

直接上代码:

public class ClassPathTest {

    public static void main(String[] args) throws IOException {
        // resources目录下创建web.properties文件

        // 1、获取classpath根目录下的web.properties,读取properties文件最简单的方式
        ResourceBundle properties = ResourceBundle.getBundle("web");
        for(String key : properties.keySet()) {
            System.out.println(key+"="+properties.getString(key));
        }

        // 2、使用ClassPathResource
        URL classPathUrl =  new ClassPathResource("/web.properties").getURL();
        UrlResource classPathResource = new UrlResource(classPathUrl);
        Properties classPathProperties = PropertiesLoaderUtils.loadProperties(classPathResource);
        classPathProperties.forEach((key,value) ->
                System.out.println(key+"="+value)
        );

        // 3、使用Class.getResource,路径支持绝对路径和相对路径
        URL classUrl = ClassPathResource.class.getResource("/web.properties");
        // 还支持获取InputStream流
        InputStream classInputStream = ClassPathResource.class.getResourceAsStream("/web.properties");
        UrlResource classResource = new UrlResource(classUrl);
        Properties classProperties = PropertiesLoaderUtils.loadProperties(classResource);
        classProperties.forEach((key,value) ->
                System.out.println(key+"="+value)
        );

        // 4、使用ClassLoader().getResource,路径最前面不能写/,默认从根目录开始读取
        URL classLoaderUrl = ClassPathResource.class.getClassLoader().getResource("web.properties");
        // 还支持获取InputStream流
        InputStream classLoaderInputStream = ClassPathTest.class.getClassLoader().
                getResourceAsStream("web.properties");
        UrlResource classLoaderResource = new UrlResource(classLoaderUrl);
        Properties classLoaderProperties = PropertiesLoaderUtils.loadProperties(classLoaderResource);
        classLoaderProperties.forEach((key,value) ->
                System.out.println(key+"="+value)
        );

        // 5、读取所有jar包下存在某个资源的
        // 读取所有jar和当前项目下classpath下存在该路径的资源的URL,springboot自动装配就是这样写的
        Enumeration<URL> urls = ClassPathResource.class.getClassLoader().getResources("META-INF/spring.factories");
        while (urls.hasMoreElements()) {
            UrlResource resource = new UrlResource(urls.nextElement());
            Properties resourcesProperties = PropertiesLoaderUtils.loadProperties(resource);
            resourcesProperties.forEach((key,value) ->
                    System.out.println(key+"="+value)
            );
        }

        // 6、基于spring ResourceUtils工具类
        URL resourceUtilsUrl = ResourceUtils.getURL("classpath:web.properties");
        UrlResource resourceUtilsResource = new UrlResource(resourceUtilsUrl);
        Properties resourceUtilsProperties = PropertiesLoaderUtils.loadProperties(resourceUtilsResource);
        resourceUtilsProperties.forEach((key,value) ->
                System.out.println(key+"="+value)
        );

    }
}

另外,不要在springboot项目下获取File对象,因为当springboot使用jar的形式启动后,File对象不能读取到jar包里面的文件,所以使用时会报错,需要转换为资源文件的方式,或者流。

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值