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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    ubuntu下安装eclipse
    UTC时间、GMT时间、本地时间、Unix时间戳
    [转]mysql使用关键字作为列名的处理方式
    mysql日期格式化
    ssh远程登陆看不到用户名和主机名
    ssh以root用户远程登录失败
    PowerBI发布到网页
    视图是否有主键的问题
    select count(*)和select count(1)
    PPT产品的重要性
  • 原文地址:https://www.cnblogs.com/AryaZ/p/10026251.html
Copyright © 2011-2022 走看看