// 第一种:
 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. 关于web.xml不同版本之间的区别

    一.Servlet 2.3 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  2. 9个步骤:教你设计出优秀的MMORPG副本关卡

    转自:http://www.gameres.com/664485.html 副本的定义 以一张场景地图为原型,针对单个玩家.队伍或者团队生成的一个实例,包含完整的开启关闭.怪物刷新.进度记录等逻辑. ...

  3. 【转】ruby中nil?, empty? and blank?的选择

    In Ruby, you check with nil? if an object is nil:article = nil article.nil? # => true empty? chec ...

  4. RabbitMQ 消息队列 安装及使用

    RabbitMQ 消息队列安装: linux版本:CentOS 7 安装第一步:先关闭防火墙 1.Centos7.x关闭防火墙 [root@rabbitmq /]# systemctl stop fi ...

  5. 12-15winform--窗体

    一.窗体:有许多控件组成,空间本身就是类对象: 1)每一个控件都有自己的属性和方法. 2)控件的方法叫做事件: 3)同一命名空间下的项目文件是一个整体文件.设计器的代码(类文件)在“解决方案管理器”- ...

  6. 问题:oracle LISTAGG 连接字符串;结果:Oracle 连接字符串的方法

    Oracle 连接字符串的方法 方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面 ...

  7. linux命令-du查看占用磁盘空间大小

    格式 df -h 查看磁盘分区情况 du /etc 目录文件大小都列出来 单位是k最后一行是总和 du -m 单位是m 小于1m写成1m du -h 单位人性化显示k/m du -sh /etc 查看 ...

  8. bitcode编译错误

    xcode编译引用的静态库可能会出现编译错误: does not contain bitcode. You must rebuild it with bitcode enabled (Xcode se ...

  9. OpenCV4Android 不需要安装OpenCVManager,就可以运行的方法

    http://blog.csdn.net/yanzi1225627/article/details/27863615 OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种 ...

  10. spring整合web项目演示