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