classpath和filepath
********************************
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();
}
}
classpath和filepath的更多相关文章
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- javac编译乱码
PersonTest.java:1: 错误: 非法字符: \65279 解决途径如下 用记事本打开java源文件,另存为ANSI格式 如果java文件包含中文字符,使用-encoding gbk格式进 ...
- 操作word
package com.gwt.flow.task; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)
项目是spring-boot + spring-cloud 并使用maven 管理依赖.在springboot+maven项目下怎么读取resources下的文件实现文件下载? 怎么获取resourc ...
- JAVA获取程序(打成jar或classpath)所在目录
一.简述 JAVA获取程序(打成jar或classpath)所在目录. 二.代码 package dearcloud.utils.context; import dearcloud.utils.Str ...
- Java自定义类加载和ClassPath类加载器
1 自定义类加载器: 实现规则: 自定义类加载器,需要重写findClass,然后通过调用loadClass进行类加载(loadClass通过递归实现类的双亲委派加载) package com.dax ...
- 再次思考 classpath 环境变量 等
f:\aspectj1.8\lib\aspectjrt.jar;.;%JAVA_HOME%\lib;C:\Temp\IBM\SQLLIB\java\db2java.zip;C:\Temp\IBM\SQ ...
- web.xml 配置中classpath: 与classpath*:的区别
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...
- CentOS下配置java环境变量classpath
一. 需要配置的环境变量1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin ...
随机推荐
- The 14th tip of DB Query Analyzer
The 14th tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporated, Guangzhou 5 ...
- 面试心得随谈&线程并发的总结
---恢复内容开始--- 线程同步有两种实现方式: 基于用户模式实现和用内核对象实现.前者偏于轻量级,性能也更好,但是只能用于同一进程间的线程同步,后者重量级,性能消耗更大,跨进程. 研读了一下win ...
- Jquery Easing函数库
从jQuery API 文档中可以知道,jQuery自定义动画的函数.animate( properties [, duration] [, easing] [, complete] )有四个参数: ...
- Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
Jsp 环境目前最流行的是 Tomcat5.0.Tomcat5.0 自己包含一个 Web 服务器,如果是测试,就没必要把 Tomcat 与 IIS 或 Apache 集成起来.在 Tomcat 自带的 ...
- HashMap 深入分析
/** *@author annegu *@date 2009-12-02 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上 ...
- linux下redis单机版搭建
1.1.什么是redis Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如下: ...
- Using Sass with the Angular CLI
https://www.tuicool.com/articles/mauiMzY One of the first things you'll usually do in a project is t ...
- win10右键添加在此处打开powershell
如图: 你想要的效果可能就是这个吧?但是找了好久没有找到方法?爸比告诉你,不需要修改任何东西, 解锁新姿势: 在文件夹空白处,按住shift同时鼠标右击,发现没??发现没!!!!
- RSAC 2018:人工智能成为驱动网络安全的新 “引擎”
作为全球顶级的权威安全会议,RSA已成为快速了解世界安全趋势的风向标,更是影响安全产业转型与持续发展的重要平台.不同于往年人工智能(AI)在安全领域更多的是一种理论探讨,今年看到的是大量人工智能在安全 ...
- 大型三甲医院信息管理系统源码 His系统功能齐全 完整可用
详情请点击查看 开发环境 :Asp.net + VS2005 + C# + SQL2010(含三种数据库access,oracle,sql server) 采用了BS+ActiveX + Web ...