// 第一种:
 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. 如何禁用 a 标签的点击事件

    a标签是没有disable属性的 ,如果想用disable 禁用a标签的点击事件,也可以实现: 1.a标签要用disable属性,必须和pointer-events属性一起使用, html代码: &l ...

  2. ansible for devops读书笔记第一章

    yum -y install ansible ansible --version mkdir /etc/ansible touch /etc/ansible/hosts [example]   www ...

  3. Markdown简要规则

    We believe that writing is about content, about what you want to say – not about fancy formatting. 我 ...

  4. day70 12-存储过程和存储函数

    什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...

  5. JavaScript toLowerCase() 方法

    定义和用法 toLowerCase() 方法用于把字符串转换为小写. 语法 stringObject.toLowerCase() 返回值 一个新的字符串,在其中 stringObject 的所有大写字 ...

  6. go 语言介绍

    特点1. 静态类型,编译开源语言 2. 脚本化的语法,支持多种编程范式(函数式,面向对象) 3. 原生,给力的并发支持并发编程 Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+ ...

  7. Spring 框架学习 有用

    1.1.1 spring的优势 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.用户也不必再为单例模式类.属性文件 ...

  8. hibernate 对象OID

    它是hibernate用于区分两个对象是否是同一个对象的标识. 我们都知道,虚拟机内存区分两个对象看的是内存的地址是否一致.数据库区分两个对象,靠的是表的主键.hibernate负责把内存中的对象持久 ...

  9. Centos7安装mysql缺乏yum源怎么安装

    找到mysql5.6的centos的repo源,终于解决mysql的安装问题: 1.确保centos安装了wget,没有的话安装wget   1 yum install wget 2.下载mysql的 ...

  10. ROS Learning-003 beginner_Tutorials 创建ROS工作空间

    ROS Indigo beginner_Tutorials-02 创建ROS工作空间 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04. ...