• idea 获取resources资源目录下文件


    以下格式都是正确的(注意:.properties文件中的第一行不要有空格!):

            URL resource01 = MainMobile.class.getResource("");
            URL resource02 = MainMobile.class.getResource("/conf.properties");//success: resources下的
    
            InputStream ip3 = MainMobile.class.getClassLoader().getResourceAsStream("conf.properties");//success: resources下的
            InputStream resource32 = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf.properties");
    
            InputStream ips =  MainMobile.class.getResourceAsStream("/conf.properties");//success: resources下的
            URL resource13 = Thread.currentThread().getContextClassLoader().getResource("conf.properties");
            InputStream resource43 = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf.properties");
            InputStream resource23 = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf.properties");
    SpringBoot读取resources目录下的文件
    
    1. 传统java读取方式
    
    File file = new File("src/main/resources/static/assets/test.txt");
    InputStream inputStream=new FileInputStream(file);
    
    2. ResourceUtils工具类
    
    File file = ResourceUtils.getFile("classpath:static/assets/test.txt");
    InputStream inputStream=new FileInputStream(file);
    
    3. ClassPathResource类
    
    ClassPathResource classPathResource = new ClassPathResource("static/assets/test.txt");
    InputStream inputStream = classPathResource.getInputStream();
  • 相关阅读:
    网络设备
    Linux常用操作
    工作常用笔记
    性能测试问题总结
    mysql性能分析
    Java8新特性学习笔记-CompletableFuture
    Java语言定义的线程状态分析
    Gatling-插件开发
    RabbitMQ记录
    Go常用库
  • 原文地址:https://www.cnblogs.com/hahajava/p/11317183.html
Copyright © 2020-2023  润新知