• 读取resources 目录资源文件的方法


    公用方法:

    1. /**
    2. * 根据文件路径读取文件内容
    3. *
    4. * @param fileInPath
    5. * @throws IOException
    6. */
    7. public static void getFileContent(Object fileInPath) throws IOException {
    8. BufferedReader br = null;
    9. if (fileInPath == null) {
    10. return;
    11. }
    12. if (fileInPath instanceof String) {
    13. br = new BufferedReader(new FileReader(new File((String) fileInPath)));
    14. } else if (fileInPath instanceof InputStream) {
    15. br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
    16. }
    17. String line;
    18. while ((line = br.readLine()) != null) {
    19. System.out.println(line);
    20. }
    21. br.close();
    22. }

    方法 1

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

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

     方法 2

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

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

    方法 3

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

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

    方法 4

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

    1. /**
    2. * 直接使用getResourceAsStream方法获取流
    3. * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
    4. *
    5. * @param fileName
    6. * @throws IOException
    7. */
    8. public void function4(String fileName) throws IOException {
    9. InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
    10. getFileContent(in);
    11. }

     方法 5

    主要也是使用getResourceAsStream方法获取流,不使用getClassLoader可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取,SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。

    1. /**
    2. * 直接使用getResourceAsStream方法获取流
    3. * 如果不使用getClassLoader,可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取
    4. *
    5. * @param fileName
    6. * @throws IOException
    7. */
    8. public void function5(String fileName) throws IOException {
    9. InputStream in = this.getClass().getResourceAsStream("/" + fileName);
    10. getFileContent(in);
    11. }

    方法 6

    通过ClassPathResource获取文件流,SpringBoot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。

    1. /**
    2. * 通过ClassPathResource类获取,建议SpringBoot中使用
    3. * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
    4. *
    5. * @param fileName
    6. * @throws IOException
    7. */
    8. public void function6(String fileName) throws IOException {
    9. ClassPathResource classPathResource = new ClassPathResource(fileName);
    10. InputStream inputStream = classPathResource.getInputStream();
    11. getFileContent(inputStream);
    12. }

  • 相关阅读:
    如何通过Appium连接真机调试
    JVM垃圾回收总结
    dubbo-admin配置及使用
    学习Java的第十六天。。。(构造方法、方法重载、this、局部变量和成员变量)
    MySQL中的死锁
    AWR不能生成报告
    windows11系统如何设置锁屏壁纸
    XSSFWorkbook Excel导出导入
    js实现数组扁平化的多种方式
    TikTok的媒体革命:新闻业如何适应短视频时代?
  • 原文地址:https://blog.csdn.net/qq_41482600/article/details/127101649