// 第一种:
 2  File f = new File(this.getClass().getResource("/").getPath());
  // 结果: /Users/admin/Documents/IdeaProjects/Demo/out/production/Demo
  System.out.println(f);

  f = new File(this.getClass().getResource("").getPath());
  // 多返回了包名
  // 结果: /Users/admin/Documents/IdeaProjects/Demo/out/production/Demo/com/company
  System.out.println(f);

  // 第二种:获取项目名
  File directory = new File("");
  String courseFile = null;
  try {
     courseFile = directory.getCanonicalPath();
  } catch (IOException e) {
     e.printStackTrace();
  }
  // 结果: /Users/admin/Documents/IdeaProjects/Demo
  System.out.println(courseFile);

  // 第三种:获取项目名
  // 结果: /Users/admin/Documents/IdeaProjects/Demo
  System.out.println(System.getProperty("user.dir"));

  // 第四种:获取项目中src目录下的一级文件路径
  URL path = this.getClass().getClassLoader().getResource("test.txt");
  System.out.println(path.getPath());

java项目中获取文件路径的几种方法的更多相关文章

  1. java中获取文件路径的几种方式

    http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...

  2. OSGI项目中获取文件路径

    假设想依据给定的文件名创建一个File实例,你可能会这么写: File file = new File(当前类.class.getResource("config").toURI( ...

  3. 关于Java获取文件路径的几种方法

    第一种:File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f);  ...

  4. Java获取文件路径的几种方法

    第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...

  5. python中获取文件路径的几种方式

    # 如果执行文件为E:\aa\bb\aa.py 1.获取当前路径 current_path11 = os.path.abspath(__file__) current_path12 = os.path ...

  6. C#获取文件路径的几种方法

    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Application.StartupPath;//可获得当前执行的exe的文件名. string str1 ...

  7. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  8. Java中获取文件路径

    Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...

  9. Android中获取文件路径的方法总结及对照

    最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...

随机推荐

  1. MySQL 查询数据表里面时间字段为今天添加的计数

    一: 下面这条语句查出来的count值 .  查询类型ID(category_id)为18的,今天插入的数据数, created_on: 为数据表中一字段 datetime类型, 记录此条数据添加的时 ...

  2. [转]ubuntu 网络配置 作者:Yudar

    检查网络配置命令:ifconfig 一.通过配置文件配置 新手没怎么用过Ubuntu,所以走了不少弯路,网上找了很多方法,大都没对我起到帮助作用,所以把自己的配置方法写一写. Ubuntu上连了两块网 ...

  3. [python] 关于错误 ImportError: cannot import name compile_command

    我的pydev某一天开始就无法debug 了,执行debug 就会报 ImportError: cannot import name compile_command 原因居然是:我自己写了一个code ...

  4. struts2中常用constant命令配置

    struts.objectFactory这个属性用 于说明Struts2的 对象池创建工厂,Struts2也有自己的对象池,就像Spring那样,在配置文件中你可以引用对象池中的对象,你可以借助于Sp ...

  5. phonegap中使用自带浏览器打开链接

    <center><a id="ssl2" href="#" onclick="window.open('http://127.0.0 ...

  6. [Apache]如何查看apache服务器的error log(错误日志)

    在进行网页和服务器的测试时, 有时会提醒 500 Internal Server Error: The server encountered an internal error or misconfi ...

  7. MySQL启动失败解决办法

    本人大二,进了一家培训机构学了两年,然后跟着他们做项目,其实也是练习,然后过程中遇到好多错误,当时解决了,然后下次遇到又忘了,-_-!  -_-||| 我就想通过写博客来记载一下我遇到的错误和解决的办 ...

  8. 空中楼阁 ( House )最短路

    题目描述: 话说Z4阴差阳错地来到了神秘岛.不久,他们发现,这是一个由n个小岛和一个中心岛组成的群岛,群岛之间有m座桥.令他们感到惊讶的是,这些桥并不是固定不变的,经较长时间的观察,发现它们会随时间作 ...

  9. rm 删除文件或目录

    rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉.对于链接文件,只是删除整个链接文件,而原有文件保持不变. 注意:使用rm命令要格外小心.因为一旦 ...

  10. ROS Learning-029 (提高篇-007 A Mobile Base-05) 控制移动平台 --- (Python编程)控制虚拟机器人的移动(精确的制定目标位置)

    ROS 提高篇 之 A Mobile Base-05 - 控制移动平台 - (Python编程)控制虚拟机器人的移动(精确的制定目标位置) 使用 odometry 消息类型 重写 out_and_ba ...