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