• java获取文件路径总结


    1. 两种方式比较
    1. String path = this.getClass().getClassLoader().getResource("").getPath();
    
    • 1

    返回:/F:/develop/local_project/mq/demo/target/classes/

    2. String path1 = DemoApplication.class.getResource("").getPath();
    
    • 1

    返回:/F:/develop/local_project/mq/demo/target/classes/com/my/demo/

    区别在于classLoader获取的是类的根路径,非classLoader获取的是当前类的绝对包路径。

    3. String path1 = DemoApplication.class.getResource("/").getPath();
    
    • 1

    返回:/F:/develop/local_project/mq/demo/target/classes/

    带了“/”号后,得到的结果和classLoader是一致的。

    1. 获取src同级目录下的文件方式:
    InputStream stream = this.getClass().getClassLoader().getResourceAsStream("./file/student.txt");
    
    • 1

    目录结构:
    在这里插入图片描述
    3. 总结:
    (1)getResourceAsStream是用来获取classpath下的文件,如果编译后的配置文件不出现在classpath下面,则程序就无法获取到。

    (2)注意如果路径中带有中文会被URLEncoder,因此这里需要解码

    String filePath = URLDecoder.decode(path, "UTF-8");
    
    • 1

    (3)直接使用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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    也可以使用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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    也可以通过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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (4)通过new File(“”)获取当前的绝对路径,只是本地绝对路径,不能用于服务器获取。

  • 相关阅读:
    【第2期赠书活动】〖Python 数据库开发实战 - Redis篇⑤〗- Redis 的常用配置参数
    Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
    女生做软件测试会不会成为一个趋势?
    一篇解决登录与支付
    【医学影像处理】基于MRIcron的dcm2nii批量dcm转nii格式
    大咖说*计算讲谈社|自动驾驶,未来的移动智能载体?
    金融行业多活架构设计及容灾发展趋势
    看我为了水作业速通 opengl freeglut!
    springboot配置静态资源访问
    【Linux】:使用git命令行 || 在github创建项目 || Linux第一个小程序——进度条(进阶版本)
  • 原文地址:https://blog.csdn.net/qq_38747892/article/details/126751734