SpringBoot–基础–10–读取resources目录下的文件
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/springboot-learn/springboot-1
1、10种方式
1.1、使用 ClassLoader.getResourceAsStream() 方法
- 可以使用类加载器来获取资源文件的输入流。
- 该方法接受一个资源文件路径参数,返回一个 InputStream 对象。
- 该方法返回的资源文件路径是相对于类加载器的根路径。因此,对于 resources 目录下的文件,需要在文件名前加上 “classpath:” 前缀。例如: “classpath:file.txt”。
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.txt");
1.2、使用 Class.getResourceAsStream() 方法
- 可以使用 Class 类的 getResourceAsStream() 方法来读取资源文件。
- 该方法接受一个资源文件路径参数,返回一个 InputStream 对象。
- 该方法返回的资源文件路径是相对于当前类的路径。因此,对于 resources 目录下的文件,需要在文件名前加上 “/” 前缀。例如: “/file.txt”。
InputStream inputStream = getClass().getResourceAsStream("/file.txt");
1.3、使用 ResourceLoader 加载文件
可以使用 Spring 的 ResourceLoader 接口来加载资源文件。
ResourceLoader 接口有一个 getResource() 方法,接受一个资源文件路径参数,返回一个 Resource 对象。
@Autowired
private ResourceLoader resourceLoader;
Resource resource = resourceLoader.getResource("classpath:file.txt");
InputStream is3 = resource.getInputStream();
System.out.println("使用 ResourceLoader 加载文件:" + is3);
1.4、使用 ResourceUtils 加载文件
ResourceUtils 是 Spring 提供的一个工具类,用于加载资源文件。
可以使用 ResourceUtils.getFile() 方法来获取文件对象。
该方法只适用于本地文件系统和 JAR 文件。对于 WAR 文件或者其他类型的文件,该方法可能无法正常工作。
File file = ResourceUtils.getFile("classpath:file.txt");
1.5、使用 ApplicationContext 加载文件
可以使用 ApplicationContext 的 getResource() 方法来加载资源文件。
该方法接受一个资源文件路径参数,返回一个 Resource 对象。
@Autowired
private ApplicationContext applicationContext;
Resource resource2 = applicationContext.getResource("classpath:file.txt");
InputStream is4 = resource2.getInputStream();
System.out.println("使用 ApplicationContext 加载文件:" + is4);
1.6、使用 ServletContext 加载文件
可以使用 ServletContext 的 getResourceAsStream() 方法来读取资源文件。
该方法接受一个资源文件路径参数,返回一个 InputStream 对象。
@Autowired
private ServletContext servletContext;
InputStream is5 = servletContext.getResourceAsStream("/classes/file.txt");
1.7、使用 File System 加载文件
可以使用 File 类来读取资源文件。需要提供完整的文件路径。
使用该方法需要提供完整的文件路径,因此需要知道文件所在的绝对路径。
File file2 = new File("D:\\java\\workSpace-learn\\learnDemo\\springboot-learn\\springboot-5\\src\\main\\resources\\file.txt");
InputStream is6 = new FileInputStream(file2);
1.8、使用 Paths 和 Files 加载文件
可以使用 Java NIO 中的 Paths 和 Files 类来读取资源文件。该方法需要提供完整的文件路径。
使用该方法需要提供完整的文件路径,因此需要知道文件所在的绝对路径。
Path path = Paths.get("D:\\java\\workSpace-learn\\learnDemo\\springboot-learn\\springboot-5\\src\\main\\resources\\file.txt");
InputStream is7 = Files.newInputStream(path);
1.9、使用 ClassPathResource 加载文件
可以使用 Spring 提供的 ClassPathResource 类来读取资源文件。
该方法需要提供资源文件的相对路径。
ClassPathResource 会在类路径下查找资源文件,因此不需要提供完整的文件路径。
ClassPathResource resource = new ClassPathResource("file.txt");
InputStream inputStream = resource.getInputStream();
1.10、绝对路径加载
String filePath = getClass().getResource("/").getPath() + "input/input.txt";
2、案例
package fei.zhou.springboot5.business.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
public class HelloController {
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private ServletContext servletContext;
@RequestMapping(value = "sayHello")
public String sayHello() throws Exception {
InputStream is1 = getClass().getClassLoader().getResourceAsStream("file.txt");
System.out.println("使用 ClassLoader.getResourceAsStream() 方法:" + is1);
InputStream is2 = getClass().getResourceAsStream("/file.txt");
System.out.println("使用 Class.getResourceAsStream() 方法:" + is2);
Resource resource = resourceLoader.getResource("classpath:file.txt");
InputStream is3 = resource.getInputStream();
System.out.println("使用 ResourceLoader 加载文件:" + is3);
File file = ResourceUtils.getFile("classpath:file.txt");
System.out.println("使用 ResourceUtils 加载文件:" + file.exists());
Resource resource2 = applicationContext.getResource("classpath:file.txt");
InputStream is4 = resource2.getInputStream();
System.out.println("使用 ApplicationContext 加载文件:" + is4);
InputStream is5 = servletContext.getResourceAsStream("/classes/file.txt");
System.out.println("使用 ServletContext 加载文件:" + is5);
File file2 = new File("D:\\java\\workSpace-learn\\learnDemo\\springboot-learn\\springboot-5\\src\\main\\resources\\file.txt");
InputStream is6 = new FileInputStream(file2);
System.out.println("使用 File System 加载文件:" + is6);
Path path = Paths.get("D:\\java\\workSpace-learn\\learnDemo\\springboot-learn\\springboot-5\\src\\main\\resources\\file.txt");
InputStream is7 = Files.newInputStream(path);
System.out.println("使用 Paths 和 Files 加载文件:" + is7);
ClassPathResource resource3 = new ClassPathResource("file.txt");
InputStream is8 = resource3.getInputStream();
System.out.println("使用 ClassPathResource 加载文件:" + is8);
String filePath = getClass().getResource("/").getPath() + "input/input.txt";
System.out.println("绝对路径加载:" + filePath);
return "hello";
}
}
输出:
使用 ClassLoader.getResourceAsStream() 方法:java.io.BufferedInputStream@57f62d0a
使用 Class.getResourceAsStream() 方法:java.io.BufferedInputStream@60df8d35
使用 ResourceLoader 加载文件:java.io.BufferedInputStream@3821ecae
使用 ResourceUtils 加载文件:true
使用 ApplicationContext 加载文件:java.io.BufferedInputStream@77666c74
使用 ServletContext 加载文件:null
使用 File System 加载文件:java.io.FileInputStream@119e81ef
使用 Paths 和 Files 加载文件:sun.nio.ch.ChannelInputStream@db410cc
使用 ClassPathResource 加载文件:java.io.BufferedInputStream@3a47d948
相对路径加载:/D:/java/workSpace-learn/learnDemo/springboot-learn/springboot-5/target/classes/input/input.txt