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相对路径、绝对路径及类路径的更多相关文章

  1. [JAVA] JAVA 类路径

    Java 类路径 类路径是所有包含类文件的路径的集合. 类路径中的目录和归档文件是搜寻类的起始点. 虚拟机搜寻类 搜寻jre/lib和jre/lib/ext目录中归档文件中所存放的系统类文件 搜寻再从 ...

  2. jdbc java数据库连接 6)类路径读取——JdbcUtil的配置文件

    之前的代码中,以下代码很多时候并不是固定的: private static String url = "jdbc:mysql://localhost:3306/day1029?useUnic ...

  3. Java类路径

    Java 类路径告诉 java 解释器和 javac 编译器去哪里找它们要执行或导入的类. 类(您可能注意到的那些 *.class 文件)可以存储在目录或 jar 文件中,或者存储在两者的组合中, 但 ...

  4. java类路径classpath和包

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  5. java类路径classpath

    java编译器编译.java文件和java虚拟机执行.class文件时的路径和写法不一样. 在没有设置任何classpath环境变量的情况下,javac可以编译全路径的.java文件.例如: java ...

  6. java获取当前项目或类路径

    // 获取当前项目的目录 File directory = new File("");// 参数为空 String courseFile = directory.getCanoni ...

  7. maven打包报错:在类路径或引导类路径中找不到程序包 java.lang

    刚下了个新项目,跑了下maven报错了: E:\workspace\portalframe>mvn clean install [INFO] Scanning for projects... [ ...

  8. Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新

    Java日志组件logback使用:加载非类路径下的配置文件并设置定时更新 摘自: https://blog.csdn.net/johnson_moon/article/details/7887449 ...

  9. java 获取类路径

    package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...

随机推荐

  1. 通过 curl 传递数据

    方法一(若为post方式,只适用于一维数组) /** * curl发送htpp请求 * 可以发送https,http,get方式,post方式,post数据发送 */ public function ...

  2. 11-30 js高级

    1.事件: 浏览器客户端上客户触发的行为都称为事件 所有的事件都是天生自带的,不需要我们去绑定,只需要我们去触发. 通过obj.事件名=function(){} 事件名:onmouseover onm ...

  3. OPC_Data Access Automation Interface Standard V2.02

    文库地址: https://wenku.baidu.com/view/a70d1ad4b14e852458fb57da.html

  4. week 10 blog

    一.Iterations : 1.do...while : 创建执行指定语句的循环,直到测试条件评估为false.在执行语句后评估条件,导致指定语句至少执行一次. 例子:在以下示例中,do...而循环 ...

  5. 如何移除HTML5 input在type="number"时的上下小箭头

      在chrome下: input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{    -webkit-appearance ...

  6. APB协议

    https://wenku.baidu.com/view/2663f629ef06eff9aef8941ea76e58fafab04592.html https://www.cnblogs.com/l ...

  7. [CSS] Useful CSS tool for Web designer and developer

    1. Color Picker (Chrome) You might know how to use color picker in Chrome, recently there is a featu ...

  8. IIS 网站 HTTP 转 HTTPS

    最近需要做 http 链接转成 https 链接,所以就去弄了,现在记录下: 1.准备SSL证书 最开始的时候用的是腾讯云的免费证书,有效期1年,但只能绑定一个二级域名.测试成功后,就去阿里云购买了证 ...

  9. Tomcat 9.0 安装配置

    本文转自:http://blog.sina.com.cn/s/blog_15126e2170102w5o8.html 一.JDK的安装与配置 1.从官网下载jdk,注意是jdk不是jre.最好从官网下 ...

  10. 使用Git Subtree在多个项目中共用同一个子项目

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8427796.html 场景一:已有一个大项目,需要把其中一部分内容独立出来作为共用子项目,被其他项目引用 一: ...