Java 读取resources下的文件

读取resources下的资源文件

文件如图:
在这里插入图片描述

工具包

<!-- commons-io io的工具包 -->
<dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.6</version>
 </dependency>


 <!--junit4 单元测试-->
 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
 </dependency>

1、通过ClassLoader读取文件

 /**
     * 通过ClassLoader读取文件
     */
    @Test
    public void loadPropertiesFile() throws IOException {
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("application.properties"));
        System.out.println("端口号:" + properties.getProperty("server.port"));
    }

效果:

在这里插入图片描述

2、getClassLoader获取在转成输入流

//加载资源文件
        ClassPathResource classPathResource = new ClassPathResource("excelfile/" + "07版.xlsx");
        //获取文件
        File file = classPathResource.getFile();
        //获取路径
        String path = classPathResource.getPath();
        System.out.println("path:" + path);
        System.out.println("file:" + file);
        //转成输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
        //获取文件流
        FileInputStream inputStream = new FileInputStream(file);

效果:
在这里插入图片描述
3、ResourceUtils获取文件

/**
     * ResourceUtils获取文件
     */
    @Test
    public void loadFile02() throws IOException {
        //获取文件的URL
        File file = ResourceUtils.getFile("classpath:excelfile/03版.xls");
        System.out.println("文件:" + file);
        //转成string输入文本
        String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
        System.out.println("内容:" + content);
    }

效果:
在这里插入图片描述

完整示例:

package sqy.controller;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @author suqinyi
 * @Date 2022/4/14
 * 测试读取resources目录下的文件
 */
public class TestGetResourcesFileController {

    /**
     * 通过ClassLoader读取文件
     */
    @Test
    public void loadPropertiesFile() throws IOException {
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("application.properties"));
        System.out.println("端口号:" + properties.getProperty("server.port"));
    }

    /**
     * getClassLoader获取在转成输入流
     */
    @Test
    public void loadFile01() throws IOException {
        //加载资源文件
        ClassPathResource classPathResource = new ClassPathResource("excelfile/" + "07版.xlsx");
        //获取文件
        File file = classPathResource.getFile();
        //获取路径
        String path = classPathResource.getPath();
        System.out.println("path:" + path);
        System.out.println("file:" + file);
        //转成输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
        //获取文件流
        FileInputStream inputStream = new FileInputStream(file);
    }

    /**
     * ResourceUtils获取文件
     */
    @Test
    public void loadFile02() throws IOException {
        //获取文件的URL
        File file = ResourceUtils.getFile("classpath:excelfile/03版.xls");
        System.out.println("文件:" + file);
        //转成string输入文本
        String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
        System.out.println("内容:" + content);
    }


}

  • 7
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Java中,可以使用以下步骤来读取resources中的JSON文件: 1. 首先,确保将JSON文件放置在src/main/resources目录下,这是Java默认的资源文件夹。 2. 使用`ClassLoader`类的`getResourceAsStream`方法来获取JSON文件的输入流。代码示例如下: ``` InputStream inputStream = getClass().getClassLoader().getResourceAsStream("filename.json"); ``` 这里的`filename.json`是你要读取的JSON文件文件名。 3. 使用`BufferedReader`类来读取输入流中的内容。代码示例如下: ``` BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ``` 4. 使用`StringBuilder`类来构建JSON文件的内容。代码示例如下: ``` StringBuilder jsonContent = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { jsonContent.append(line); } ``` 5. 最后,将读取到的JSON内容转换为标准的JSON对象。你可以使用任何适合的JSON库来完成此操作,例如`JSONObject`类或`Gson`库。 综上所述,以上是读取resources中的JSON文件的步骤。你可以根据你的具体需求进行适当的调整和扩展。<span class="em">1</span> #### 引用[.reference_title] - *1* [java怎么读取resources下面的JSON串文件](https://blog.csdn.net/laughing1997/article/details/103589602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

suqinyi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值