各种读取resources目录下文件的方法(图文解释)

2年前 (2022) 程序员胖胖胖虎阿
185 0 0

各种读取resources目录下文件的方法

  • 引言
  • 读取展示文件的工具方法
  • 先获取路径再去获取文件
    • 准备:getFile() getPath()须知
    • getResource("").getPath()获取resource路径
    • getResource(fileName).getPath()直接获取完整路径
    • getResource(fileName).getFile()获取完整路径
    • System.getProperty("user.dir")(不能在web应用使用)
    • getCanonicalPath();getAbsolutePath()
  • 直接去获取流
    • getResourceAsStream获取流
    • 不使用getClassLoader获取流
    • ClassPathResource类获取流

引言

  我们写使用java写web项目时,有时需要将某些文件存放到resources目录下,之后我们需要在程序中去获取文件。此时就有一个重要的事情:如何获取文件的路径?
  下面我将给出八种获取resources的方式。

注意:有的可以在web项目中使用,有的则不可以在web项目中使用。

读取展示文件的工具方法

首先给出读取展示 文件的代码块:

/**
     * 需要有明确路径,因为需要输出显示,所以最好是文本文档:“.txt”、“.html”、“.js”等
     * @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();
    }

先获取路径再去获取文件

准备:getFile() getPath()须知

  我们使用:

this.getClass().getClassLoader().getResource("").[getPath()/getFile()];

这个命令去获取resource的路径,下面我打断点,可以看到resource的内容,里面有file,path。
各种读取resources目录下文件的方法(图文解释)
如果是文件路径的话getFile和getPath效果是一样的,如果是URL路径的话getFile是带有参数的路径。
例如:
各种读取resources目录下文件的方法(图文解释)

getResource(“”).getPath()获取resource路径

/**
     * 读取resources下的文件,getResource("")然后拼接
     * @param fileName
     * @throws IOException
     */
    public void function1(String fileName)throws IOException{
        String path = this.getClass().getClassLoader().getResource("").getPath();
        path = URLDecoder.decode(path,"UTF-8");
        System.out.println(path);
        String filePath = path + fileName;
        System.out.println(filePath);
        getFileContent(filePath);
    }

getResource(fileName).getPath()直接获取完整路径

/**
     * getResource(fileName)直接获取完整路径
     * @param fileName
     * @throws IOException
     */
    public void function2(String fileName)throws IOException{
        String path = this.getClass().getClassLoader().getResource(fileName).getPath();
        System.out.println(path);
        String filePath = URLDecoder.decode(path,"UTF-8");
        System.out.println(filePath);
        getFileContent(filePath);
    }

getResource(fileName).getFile()获取完整路径

/**
     * getFile()
     * @param fileName
     * @throws IOException
     */
    public void function3(String fileName)throws IOException{
        String path = this.getClass().getClassLoader().getResource(fileName).getFile();
        System.out.println(path);
        String filePath = URLDecoder.decode(path,"UTF-8");
        System.out.println(filePath);
        getFileContent(filePath);
    }

System.getProperty(“user.dir”)(不能在web应用使用)

/**
     * user.dir默认会定位到你的工程根目录,但是web项目不行如果是web项目会定位到tomcat的bin目录
     * @param fileName
     * @throws IOException
     */
    public void function7(String fileName)throws IOException{
        String rootPath = System.getProperty("user.dir");
        String filePath = rootPath+"/src/main/resources/"+fileName;
        getFileContent(filePath);
    }

getCanonicalPath();getAbsolutePath()

/**
     * 不能在web应用中使用。
     * @param fileName
     * @throws IOException
     */
    public void function8(String fileName)throws IOException{
        File directory = new File("");
        //规范路径:getCanonicalPath()方法返回绝对路径
        String rootCanonicalPath = directory.getCanonicalPath();
        //绝对路径:getAbsolutePath()方法返回文件的绝对路径,如果创建的时候使用的绝对路径返回绝对路径,创建的时候使用的相对路径,返回相对路径
        String rootAbsolutePath = directory.getAbsolutePath();
        System.out.println(rootCanonicalPath);
        System.out.println(rootAbsolutePath);
        String filePath = rootCanonicalPath+"/src/main/resources/"+fileName;
        getFileContent(filePath);
    }

直接去获取流

  现在主流的部署方式是将项目打成jar包部署。而在jar包中是没有路径的,因为系统会认为jar包是个文件。所以我们就要通过流的方式去获取文件。

getResourceAsStream获取流

/**
     * 直接获取流数据
     * @param fileName
     * @throws IOException
     */
    public void function(String fileName)throws IOException{
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
        getFileContent(in);
    }

不使用getClassLoader获取流

/**
     * 不使用getClassLoader,直接获取resources下的文件。需要前面添加”/“
     * @param fileName
     * @throws IOException
     */
    public void function(String fileName)throws IOException{
        InputStream in = this.getClass().getResourceAsStream("/"+fileName);
        getFileContent(in);
    }

ClassPathResource类获取流


    /**
     * 通过ClassPathResource类获取,建议SpringBoot中使用
     * @param fileName
     * @throws IOException
     */
    public void function6(String fileName)throws IOException{
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream in = classPathResource.getInputStream();
        getFileContent(in);
    }

相关文章

暂无评论

暂无评论...