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);
    }


}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

suqinyi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值