// 第一种:
 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. phpstorm win/mac git配置 破解

    http://blog.csdn.net/fenglailea/article/details/53350080 phpstorm中git配置教程: http://blog.csdn.net/knig ...

  2. 反射ORM

    七章    反射 1节获取dll文件中的type---------------------------------------------------------------------------- ...

  3. springboot springcloud eureka

    参考: https://www.cnblogs.com/skyblog/p/5133752.htmlhttp://blog.csdn.net/u012734441/article/details/78 ...

  4. debug时打到了URLClassLoader.class里面,

    一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.

  5. [置顶] 都是类型惹的祸——小心unsigned

    正如我们所知道的,编程语句都有很多的基本数据类型,如char,inf,float等等,而在C和C++中还有一个特殊的类型就是无符号数,它由unsigned修饰,如unsigned int等.大家有没想 ...

  6. SQL基础(1)

    1.SQL简介 (1)什么是SQL? SQL指结构化查询语言 SQL使我们有能力访问数据库 SQL是一种 ANSI 的标准计算机语言 (2)SQL 能做什么? SQL面向数据库执行查询 SQL可从数据 ...

  7. Appium—python_ 安卓手机划屏幕操作

    开始的时候 不知道 python_unittest框架的命名规则,导致方法进不去,后来 改变方法名 能获取 # conding=utf- from appium import webdriver im ...

  8. 第六章 Java性能调优工具(待续)

    Java性能调优工具 Windows工具 JDK命令行工具 JConsole工具 Visual VM多合一工具 Visual VM对QQL的支持 MAT内存分析工具 MAT对QQL的支持 JProfi ...

  9. 数据库连接池在Tomcat中的几种配置方法

    数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标,数据库连接池正是针对这个问题提出的. ...

  10. c:if标签数据回显判断是否选中

    <form action="/brand/list.do" method="post" style="padding-top:5px;" ...