获取 /resources 目录资源文件的 9 种方法,还有谁不会?!-1

简介: 获取 /resources 目录资源文件的 9 种方法,还有谁不会?!

项目开发中,经常会有一些静态资源,被放置在resources目录下,随项目打包在一起,代码中要使用的时候,通过文件读取的方式,加载并使用;

本文中汇总整理了九种方式获取resources目录下文件的方法。

其中公用的打印文件方法如下:

/**
 * 根据文件路径读取文件内容
 *
 * @param fileInPath
 * @throws IOException
 */
public static void getFileContent(Object fileInPath) throws IOException {
    BufferedReader br = null;
    if (fileInPath == null) {
        return;
    }
    if (fileInPath instanceof String) {
        br = new BufferedReader(new FileReader(new File((String) fileInPath)));
    } else if (fileInPath instanceof InputStream) {
        br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
    }
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
}

推荐一个开源免费的 Spring Boot 最全教程:


https://github.com/javastacks/spring-boot-best-practice


方式一

主要核心方法是使用getResource和getPath方法,这里的getResource("")里面是空字符串

public void function1(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = path + fileName;
    System.out.println(filePath);
    getFileContent(filePath);
}

方式二

主要核心方法是使用getResourcegetPath方法,直接通过getResource(fileName)方法获取文件路径,注意如果是路径中带有中文一定要使用URLDecoder.decode解码。

/**
 * 直接通过文件名getPath来获取路径
 *
 * @param fileName
 * @throws IOException
 */
public void function2(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
    System.out.println(filePath);
    getFileContent(filePath);
}

方式三

直接通过文件名+getFile()来获取文件。如果是文件路径的话getFilegetPath效果是一样的,如果是URL路径的话getPath是带有参数的路径。

如下所示:

url.getFile()=/pub/files/foobar.txt?id=123456
url.getPath()=/pub/files/foobar.txt

使用getFile()方式获取文件的代码如下:

/**
 * 直接通过文件名+getFile()来获取
 *
 * @param fileName
 * @throws IOException
 */
public void function3(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
    System.out.println(path);
    String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
    System.out.println(filePath);
    getFileContent(filePath);
}

方式四(重要)

直接使用getResourceAsStream方法获取流,上面的几种方式都需要获取文件路径,但是在SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。

/**
 * 直接使用getResourceAsStream方法获取流
 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
 *
 * @param fileName
 * @throws IOException
 */
public void function4(String fileName) throws IOException {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
    getFileContent(in);
}
相关文章
|
1月前
|
Java 容器
SpringBoot读取resources下的文件以及resources的资源路径
SpringBoot读取resources下的文件以及resources的资源路径
52 0
|
7月前
|
Java Maven
maven 打包时包含resource静态文件
maven 打包时包含resource静态文件
|
8月前
|
Cloud Native Java Go
Springboot 获取 /resources 目录资源文件的 9 种方法
Springboot 获取 /resources 目录资源文件的 9 种方法
1119 0
|
9月前
|
C++
vs更改resources之后,产生新的resources1文件
vs更改resources之后,产生新的resources1文件
|
JavaScript Java Maven
如何访问jar包下面的资源文件
有些时候,我们需要通用一些功能页面,比如maven中公用的core的jar包内需要增加一些通用的功能,现在总结一下自己从中学到的一些东西
99 1
|
Java 开发工具 git
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!-2
获取 /resources 目录资源文件的 9 种方法,还有谁不会?!
|
Java Maven
Maven项目获取资源文件路径并读取资源文件内容
Maven项目获取资源文件路径并读取资源文件内容
231 0
Maven项目获取资源文件路径并读取资源文件内容
|
C#
C# .Resx文件无效解决方案 Resx文件报错解决方法
C# .Resx文件无效解决方案 Resx文件报错解决方法
640 7
C# .Resx文件无效解决方案 Resx文件报错解决方法
C#获取资源文件夹中的完整路径
C#获取资源文件夹中的完整路径
|
消息中间件 JavaScript 小程序
九种方式,教你读取 resources 目录下的文件路径
本文中提供了九种方式获取resources目录下文件的。其中打印文件的方法如下