关于java:获取-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

形式一

次要外围办法是应用getResourcegetPath办法,这里的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);
}

形式五(重要)

次要也是应用getResourceAsStream办法获取流,不应用getClassLoader能够应用getResourceAsStream("/配置测试.txt")间接从resources根门路下获取,SpringBoot中所有文件都在jar包中,没有一个理论的门路,因而能够应用以下形式。

/**
 * 间接应用getResourceAsStream办法获取流
 * 如果不应用getClassLoader,能够应用getResourceAsStream("/配置测试.txt")间接从resources根门路下获取
 *
 * @param fileName
 * @throws IOException
 */
public void function5(String fileName) throws IOException {
    InputStream in = this.getClass().getResourceAsStream("/" + fileName);
    getFileContent(in);
}

最新 Spring 面试题整顿好了:https://www.javastack.cn/spring-mst/

形式六(重要)

通过ClassPathResource类获取文件流,SpringBoot中所有文件都在jar包中,没有一个理论的门路,因而能够应用以下形式。

/**
 * 通过ClassPathResource类获取,倡议SpringBoot中应用
 * springboot我的项目中须要应用此种办法,因为jar包中没有一个理论的门路寄存文件
 *
 * @param fileName
 * @throws IOException
 */
public void function6(String fileName) throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(fileName);
    InputStream inputStream = classPathResource.getInputStream();
    getFileContent(inputStream);
}

形式七

通过绝对路径获取我的项目中文件的地位,只是本地绝对路径,不能用于服务器获取。

/**
 * 通过绝对路径获取我的项目中文件的地位(不能用于服务器)
 * @param fileName
 * @throws IOException
 */
public void function7(String fileName) throws IOException {
    String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-example
    String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
    getFileContent(filePath);
}

形式八

通过new File("")获取以后的绝对路径,只是本地绝对路径,不能用于服务器获取。

/**
 * 通过绝对路径获取我的项目中文件的地位(不能用于服务器)
 * @param fileName
 * @throws IOException
 */
public void function8(String fileName) throws IOException {
    //参数为空
    File directory = new File("");
    //标准门路:getCanonicalPath() 办法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
    String rootCanonicalPath = directory.getCanonicalPath();
    //绝对路径:getAbsolutePath() 办法返回文件的绝对路径,如果结构的时候是全门路就间接返回全门路,如果结构时是相对路径,就返回当前目录的门路 + 结构 File 对象时的门路
    String rootAbsolutePath =directory.getAbsolutePath();
    System.out.println(rootCanonicalPath);
    System.out.println(rootAbsolutePath);
    String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
    getFileContent(filePath);
}

形式九

次要是通过设置环境变量,将文件放在环境变量中,原理也是通过绝对路径获取。

示例中我设置了一个环境变量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-example

 System.getenv("TEST_ROOT");
 System.getProperty("TEST_ROOT")

通过设置环境变量的形式,而后通过绝对路径获取文件

/**
 * 通过绝对路径获取我的项目中文件的地位
 *
 * @param fileName
 * @throws IOException
 */
public void function9(String fileName) throws IOException {
    System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");
    //参数为空
    String rootPath = System.getProperty("TEST_ROOT");
    System.out.println(rootPath);
    String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
    getFileContent(filePath);
}

版权申明:本文为CSDN博主「leo825…」的原创文章,遵循CC 4.0 BY-SA版权协定,转载请附上原文出处链接及本申明。原文链接:https://blog.csdn.net/u011047…

近期热文举荐:

1.1,000+ 道 Java面试题及答案整顿(2022最新版)

2.劲爆!Java 协程要来了。。。

3.Spring Boot 2.x 教程,太全了!

4.别再写满屏的爆爆爆炸类了,试试装璜器模式,这才是优雅的形式!!

5.《Java开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞+转发哦!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理