1.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. 反雙斜杠'\\',運行時打印字符串時會變成'\'。
* 正斜杠,運行時打印字符串,打印結果和編譯前一致。
*/
}
以上代碼內容復制:
