zoukankan      html  css  js  c++  java
  • Java读取resource文件/路径的几种方式


    方式一:

    String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文件路径
    String fileUtl = this.getClass().getResource("文件名").getFile();
    (在项目打成jar后的情况下getPath()与getFile()返回参数及用法的基本相同具体差异大研究)
    示例路径结果:/E:/idea_work/sofn-qry-web/target/classes/CityJson.js

    方式二:

    File directory = new File("");//参数为空
    String courseFile = directory.getCanonicalPath()//标准的路径 ;
    String author =directory.getAbsolutePath()//绝对路径;
    (在jdk1.1后就有了此方法获取文件路径的方式存在了)
    示例路径结果:E:idea_worksofn-qry-web

    方式三:

    java.net.URL uri = this.getClass().getResource("/");
    (获取到Class文件存放的路径)
    示例路径结果:file:/E:/idea_work/sofn-qry-web/target/test-classes/

    String property =System.getProperty("user.dir");
    方式四:

    String property =System.getProperty("user.dir");
    (此方法可以得到该工程项目所有文件的相关路径及环境配置信息)
    示例输出结果:


    ---------------------
    作者:N先生
    来源:CSDN
    原文:https://blog.csdn.net/oschina_40188932/article/details/78833754
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    PHP程序员7小时学会Kotlin 第二小时
    PHP程序员7小时学会Kotlin系列
    PHP程序员7小时学会Kotlin系列
    技术人员如何形成正确的价值观
    景德镇特色的部门级别与权限
    hhvm的正确安装姿势 http://dl.hhvm.com 镜像
    有钱没钱
    SB心结
    优秀的技术Leader
    BaaS模式的开发思路
  • 原文地址:https://www.cnblogs.com/AryaZ/p/10026251.html
Copyright © 2011-2022 走看看