Springboot/Java读取文件

时间:2021/4/2 15:14:00来源:互联网 作者:flyso 点击: 1005 次

====工程结构图



///java属性和加载器//


  /**

         *  当测试类在 com.spt.example 下

         *  getResourceAsStream("test.txt") 会从com.spt.example包下开始找test.txt

         *  getResourceAsStream("/test.txt") 会从classpath根下开始找test.txt

         */

        this.getClass().getClassLoader().getResourceAsStream("test.txt");

        /**

         * 不在乎是否/test.txt 或者test.txt 都会从classpath根下找

         */

        ClassLoader.getSystemResourceAsStream("text.txt");

 

        /**

         * bootstrap classloader :负责加载JAVA核心类( jre 下lib和class目录中的内容)

         * extension classloader :负责加载JAVA扩展类(jre 下lib/ext 目录中的内容)

         * system classloader :负责加载应用指定的类 (环境变量classpath中配置的内容)

         */

 


   /**

         * bootstrap classloader :负责加载JAVA核心类( jre 下lib和class目录中的内容)

         * extension classloader :负责加载JAVA扩展类(jre 下lib/ext 目录中的内容)

         * system classloader :负责加载应用指定的类 (环境变量classpath中配置的内容)

         */

 

 

        /**

         * src/main/resources/test.txt 只能在开发工具中使用 打包和部署路径无效

         *

         * org.springframework.util.ResourceUtils linux下无法使用

         *

         * org.springframework.core.io.ClassPathResource jvm下所有环境均可以使用

         *

         * org.springframework.core.io.ResourceLoader; jvm下所有环境均可以使用

         */

 

————————————————

Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3