********************************

java中的相对路径和绝对路径

********************************

相对路径(其实就是编译后的路径)

——类路径:当前类所在的路径。不添加“/”则表示类加载的路径

@Test
public void classPath() {
   // 通过获取当前类的编译后所在的路径
URL classPath = getClass().getResource("");
System.out.println("classPath = " + classPath);
}
// 输出:classPath = file:/F:/etom/pmms/target/test-classes/test/

——当前类所在的根路径,添加“/”表示类加载的根路径

@Test
public void classRootPath() {
URL classRootPath= getClass().getResource("/");
System.out.println("classRootPath= " + classRootPath);
}
// classRootPath= file:/F:/etom/pmms/target/test-classes/

——项目工程所在的根路径

@Test
public void rootPath() throws IOException {
File file = new File("");
if (!file.exists()) {
file.mkdirs();
}
String rootPath = file.getCanonicalPath();
System.out.println("rootPath = " + rootPath);
}
// rootPath = F:\etom\pmms

——当前线程的类加载器获取所在的路径

@Test
public void threadPath() {
   // 通过线程的加载类所在的路径
URL threadPath = Thread.currentThread().getContextClassLoader().getResource("");
System.out.println("threadPath = " + threadPath);
}
// 输出:threadPath = file:/F:/etom/pmms/target/test-classes/

——web的相对路径

public void webPath(HttpServletRequest request) {
   // 通过获取web上下文来获取web的相对路径
String webappPath = request.getServletContext().getRealPath("");
}

绝对路径

public void filePath() {
   // 直接配置文件路径即可,记住:是全路径
File file = new File("F:/etom/pmms/target/test-classes/test/");
if (!file.exists()) {
file.mkdirs();
}
}

最新文章

  1. ionic ios 左滑 白屏
  2. Angularjs 异步模块加载项目模板
  3. https://my.oschina.net/huangyong/blog/161419
  4. Html5编辑工具
  5. html readme
  6. (转载)MatLab绘图
  7. MFC DialogBar 按钮灰色不响应
  8. CXF整合Spring开发WebService
  9. IOS中线程的通信
  10. C++中的类和对象(二)
  11. python开发_tkinter_小球完全弹性碰撞游戏
  12. centos6.5 Python.7 pip install PIL --allow-external PIL --allow-unverified PIL报错 no such option: --allow-external
  13. C# 远程传输File文件
  14. 剑指offer【02】- 替换空格(Java)
  15. RxJava2.0的使用详解
  16. P4213 【模板】杜教筛(Sum)(杜教筛)
  17. UIKit Dynamic主题学习笔记
  18. 你真的理解编码吗?unicode,utf8,utf16详解
  19. 产品开发过程描述xmind
  20. chromium之message_pump_win之二

热门文章

  1. Java不走弯路教程(5.Client-Server模式(2)-Client)
  2. 容器(list集合)
  3. Tomcat的缺省是多少,怎么修改
  4. oracle面试题目总结
  5. tar结果find打包指定后缀的文件
  6. 说说Android的MVP模式
  7. printf("Hello 2018!");
  8. ajax基本介绍
  9. python 闯关之路三(面向对象与网络编程)
  10. .NET之IOC控制反转运用