// 第一种:
 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. Oracle导出导入

    导出 exp 用户名/密码 file=文件名.dmp full=y; 导入 imp 用户名/密码 file=文件名.dmp full=y; 使用EXPDP和IMPDP时应该注意的事项: EXP和IMP ...

  2. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  3. java代码。继承。。。很戳我的心啊。。不太懂。super的真正用法

    总结: package com.addd; //构造方法不能被继承,只能是调用 //属性不能被继承,方法可以 //关于继承练习啊 class fjdsk { String name; int age; ...

  4. L2-023. 图着色问题(暴力)

    L2-023. 图着色问题 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 图着色问题是一个著名的NP完全问题.给定无向图 G ...

  5. springmvc----demo1---hello---bai

    import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import ...

  6. Repmat:Replicate and tile an array

    Repmat:Replicate and tile an array Syntax B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p. ...

  7. 最全SDWebImage-3.8版本源码阅读详解

    一.前言 SDWebImage,非常友好的网络图片加载第三方框架,在GitHub中已经获得了15000++的star,链接地址:https://github.com/rs/SDWebImage 本人分 ...

  8. JSP+Servlet 无数据库模拟登录过程

    程序目录结构: index.jsp: <%@ page language="java" contentType="text/html; charset=utf-8& ...

  9. xcode中用pods管理第三方库<转>

    安装pods :http://www.cnblogs.com/wangluochong/p/5567082.html 史上最详细的CocoaPods安装教程 --------------------- ...

  10. DAY19-Pillow制作验证码

    PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7,加上年久失修 ...