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. 反雙斜槓'\\',運行時打印字符串時會變成'\'。
     *            正斜槓,運行時打印字符串,打印結果和編譯前一致。
     */

}
相關文章
相關標籤/搜索