java相对路径、绝对路径及类路径
import java.io.File;
import java.net.URL; /**
* java相对路径、绝对路径及类路径的测试
*/
public class Test { /**
* 测试相对路径是相对谁
* -- 相对于部署项目的文件夹(AppServer)
*/
// @org.junit.Test
public void testRelativePath() throws Exception { String filePath = "test//t.txt";
File file = new File(filePath);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
} System.out.println(file.getAbsolutePath());
// E:\workspace\AppServer\test\t.txt
} /**
* 测试绝对路径
*/
// @org.junit.Test
public void testAbsolutePath() throws Exception {
String filePath = "D:\\path\\test.txt"; File file = new File(filePath);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
} System.out.println(file.getName()); // test.txt
System.out.println(file.getAbsolutePath()); // D:\path\test.txt
} /**
* 获取ClassPath(类路径)
*/
// @org.junit.Test
public void testClassPath() throws Exception {
/*
来个对比(各种情况下ClassPath的值):
1) 直接junit运行方法时打印:(给这个类单独创建了一个ClassPath)
/E:/workspace/AppServer/target/test-classes/ 2) Eclipse启动tomcat时打印(tomcat插件中的ClassPath):
/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/AppServer/WEB-INF/classes/ 3) 单独启动tomcat时打印(tomcat中的类路径):
/E:/apache-tomcat-7.0.62/webapps/AppServer/WEB-INF/classes
*/
// 获取类路径
URL url = this.getClass().getResource("/");
// file:/E:/workspace/AppServer/target/test-classes/ String path = url.getPath(); // 看看类路径下都有啥
File file = new File(path); // 直接junit运行方法
for (File f : file.listFiles()) {
System.out.println(f.getName()); // 还没有文件被编译,啥也没有
}
} /**
* 测试路径中的正反斜杠
*/
// @org.junit.Test
public void testSprit() throws Exception {
// 文件已经存在
String filePath = null; /*
* 正斜杠'/'
*/
filePath = "D:/path/test.txt"; // D:\path\test.txt
filePath = "D://path//test.txt"; // D:\path\test.txt
filePath = "D:/path//test.txt"; // D:\path\test.txt
filePath = "D:////path////test.txt"; // D:\path\test.txt /*
* 反斜杠'\'
*/
filePath = "D:\\path\\test.txt"; // D:\path\test.txt
// filePath = "D:\path\test.txt"; // 编译都通过不了啊,\t是一个制表符
// filePath = "D:\\\path\\test.txt"; // 编译都通过不了啊 // 正反斜杠混合使用
filePath = "D:\\path/test.txt"; // D:\path\test.txt
filePath = "D:/path\\test.txt"; // D:\path\test.txt File file = new File(filePath);
System.out.println(file.getAbsolutePath());
} @org.junit.Test
public void testName() throws Exception { String filePath = null; filePath = "D:/path/test.txt"; // D:/path/test.txt
System.out.println(filePath); filePath = "D://path//test.txt"; // D://path//test.txt
System.out.println(filePath); filePath = "D:/path//test.txt"; // D:/path//test.txt
System.out.println(filePath); filePath = "D:////path////test.txt"; // D:////path////test.txt
System.out.println(filePath); /*
* 反斜杠'\'
*/
filePath = "D:\\path\\test.txt"; // D:\path\test.txt
System.out.println(filePath); // 正反斜杠混合使用
filePath = "D:\\path/test.txt"; // D:\path/test.txt
System.out.println(filePath); filePath = "D:/path\\test.txt"; // D:/path\test.txt
System.out.println(filePath); } /**
* 总结:
* 1) 相对路径
*
* 相对路径:是相对于application(服务)目录所在的路径。
*
* 比如:
* 相对路径为"test/t.txt", 服务目录为:"D:/App"
* 则t.txt的绝对路径为:"D:/App/test/t.txt"
*
* 2) 绝对路径
*
* 没什么好说的。
*
* 3) 类路径
*
* a. Eclipse中右键运行(为当前类单独创建了一个类路径):
* /E:/workspace/AppServer/target/test-classes/
*
* b. Eclipse中启动tomcat(tomcat插件中的类路径)::
* /E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/AppServer/WEB-INF/classes/
*
* c. tomcat中启动start.bat(tomcat服务中的类路径):
* /E:/apache-tomcat-7.0.62/webapps/AppServer/WEB-INF/classes
*
* 4) 路径中的正反斜杠(/ \)
*
* a. '/' 正斜杠
* 怎么用都是对的,无论是单斜杠,双斜杠,多斜杠 或 混合使用,都能正确的解析文件路径。
*
* b. '\' 反斜杠
* 只能使用双斜杠'\\'.
* 单斜杠,多斜杠 或 混合使用都会报错。编译都不能通过。
*
* c. 正反斜杠混合使用
* 反斜杠只能使用双斜杠'\\', 正斜杠随意。 都能正确解析出路径。 "D:/aaa\\/bb.txt",这种写法也能解析。
*
* d. 反双斜杠'\\',运行时打印字符串时会变成'\'。
* 正斜杠,运行时打印字符串,打印结果和编译前一致。
*/ }
java相对路径、绝对路径及类路径的更多相关文章
- [JAVA] JAVA 类路径
Java 类路径 类路径是所有包含类文件的路径的集合. 类路径中的目录和归档文件是搜寻类的起始点. 虚拟机搜寻类 搜寻jre/lib和jre/lib/ext目录中归档文件中所存放的系统类文件 搜寻再从 ...
- jdbc java数据库连接 6)类路径读取——JdbcUtil的配置文件
之前的代码中,以下代码很多时候并不是固定的: private static String url = "jdbc:mysql://localhost:3306/day1029?useUnic ...
- Java类路径
Java 类路径告诉 java 解释器和 javac 编译器去哪里找它们要执行或导入的类. 类(您可能注意到的那些 *.class 文件)可以存储在目录或 jar 文件中,或者存储在两者的组合中, 但 ...
- java类路径classpath和包
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- java类路径classpath
java编译器编译.java文件和java虚拟机执行.class文件时的路径和写法不一样. 在没有设置任何classpath环境变量的情况下,javac可以编译全路径的.java文件.例如: java ...
- java获取当前项目或类路径
// 获取当前项目的目录 File directory = new File("");// 参数为空 String courseFile = directory.getCanoni ...
- maven打包报错:在类路径或引导类路径中找不到程序包 java.lang
刚下了个新项目,跑了下maven报错了: E:\workspace\portalframe>mvn clean install [INFO] Scanning for projects... [ ...
- Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新
Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新 摘自: https://blog.csdn.net/johnson_moon/article/details/7887449 ...
- java 获取类路径
package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...
随机推荐
- js将文字转化为语音并播放
js将页面中的某些文字信息转化为语音并自动播放 <!DOCTYPE html><html lang="en"><head> <meta c ...
- Ruby语法基础(一)
Ruby语法基础(一) Ruby是一种开源的面向对象程序设计的服务器端脚本语言,最初由松本行弘(Matz)设计开发,追求『快乐和生产力』,程序员友好型,被称为『human-oriented langu ...
- weak_ptr_c++11
unique_ptr 替代了原来的auto_ptr,指向对象具有唯一性,即同一时间只能有unique_ptr指向给定对象(和auto_ptr不同是禁止拷贝语义,通过移动语义替代) unique_ptr ...
- JS_高程7.函数表达式(1)
定义函数的两种常见的方法: 1 . 函数声明 2. 函数表达式 # 差异 (1)函数声明 ,具有函数声明提升的特征. (2)函数声明的函数的name属性为函数的名称:使用函数表达式定义的函数在ES5中 ...
- jQuery 学习03——HTML:捕获、设置、添加元素、删除元素、CSS类、CSS()方法、尺寸
jQuery - 获取内容text().html() 以及 val()和属性attr() jQuery 中非常重要的部分,就是操作 DOM 的能力. DOM = Document Object Mod ...
- pygame-KidsCanCode系列jumpy-part2-加速度与摩擦力
上一节,我们整理了一个游戏开发的新框架(即:Game类),本节将运用这个框架,实现基本的加速度及摩托力效果. 先定义游戏的精灵(下面代码命名为sprites.py) from part_02.sett ...
- [原创]浅谈移动互联网App兼容性测试
[原创]浅谈移动互联网App兼容性测试 今天要谈的话题,估计各位测试都有感受,移动互联网App兼容性测试,我们到底测试覆盖如何去挑选机型?具体移动App兼容性测试如何开展?是不是应引进像testin这 ...
- xinetd服务
xinetd(eXtended InterNET services daemon) 一.xinetd的功能介绍: xinetd提供类似于inetd+tcp_wrapper的功能,但是更加强大和安全.它 ...
- shiro-过滤器
http://shiro.apache.org/authorization.html#Authorization-PermissionGranularity shiro默认的过滤器 Shiro内置了很 ...
- MySQL技术内幕读书笔记(三)——文件
目录 文件 参数文件 日志文件 套接字文件 pid文件 表结构定义文件 INNODB存储引擎文件 文件 有以下类型文件 参数文件:告诉MYSQL实例启动时在哪里找到数据库文件,并且制定某些初始化参 ...