震撼揭秘:九种Java代码示例教你读取resources目录下文件路径

AI 概述
一、使用 getResource 和 getPath 方法二、根据文件路径读取文件内容三、使用 getResource 和 getPath 方法四、通过文件名+getFile()来获取文件五、通过 ClassPathResource 类获取文件流六、使用 getResourceAsStream 方法获取流(无需文件路径)七、通过绝对路径获取项目中文件的位置八、new File(“”)...
目录
文章目录隐藏
  1. 一、使用 getResource 和 getPath 方法
  2. 二、根据文件路径读取文件内容
  3. 三、使用 getResource 和 getPath 方法
  4. 四、通过文件名+getFile()来获取文件
  5. 五、通过 ClassPathResource 类获取文件流
  6. 六、使用 getResourceAsStream 方法获取流(无需文件路径)
  7. 七、通过绝对路径获取项目中文件的位置
  8. 八、new File(“”)获取当前的绝对路径
  9. 九、设置环境变量
  10. 总结

震撼揭秘:九种 Java 代码示例教你读取 resources 目录下文件路径

资源管理是软件开发过程中的重要组成部分,不容忽视。通常,项目中的资源文件包括配置文件、图片和文本文件,它们被存放在项目的 resources 目录下,以便于集中管理和访问。然而,对于初学者而言,正确地读取这些文件路径可能会带来一些困惑。本文将详细探讨 Java 中多种常见方法,用于读取 resources 目录下文件路径,旨在帮助开发者更有效地管理项目资源,并确保应用程序的鲁棒性和可维护性。

一、使用 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);
}

二、根据文件路径读取文件内容

/**
 * 根据文件路径读取文件内容
 * @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();
}

三、使用 getResource 和 getPath 方法

直接通过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=mybj
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);
}

五、通过 ClassPathResource 类获取文件流

/**
 * 通过 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);
}

六、使用 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);
}

七、通过绝对路径获取项目中文件的位置

/**
 * 通过绝对路径获取项目中文件的位置(不能用于服务器)
 * @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=D:\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);
}

总结

以上就是九种 Java 代码示例教你读取 resources 目录下文件路径的详细内容,更多请关注码云笔记其它相关文章!

以上关于震撼揭秘:九种Java代码示例教你读取resources目录下文件路径的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » 震撼揭秘:九种Java代码示例教你读取resources目录下文件路径

发表回复