日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java定位資源文件路徑

 青_春 2016-06-22

工程:GetResourceAsStreamDemo

磁盤(pán)路徑:E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo

工程目錄如下:



1.利用FIle定位:

File txtFile = new File("bin/demo/demo1/1.txt");//這里相當(dāng)于File txtFile = new File("src/demo/demo1/1.txt"); 因?yàn)閟rc的文件會(huì)build到bin中
        System.out.println("txtFile = " + txtFile);
        System.out.println("path = " + txtFile.getCanonicalPath());
        System.out.println("file = " + txtFile.isFile());


輸出:

txtFile = bin\demo\demo1\1.txt
path = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
file = true


這里,F(xiàn)ile里面的路徑,假如不從"/"開(kāi)始,就表示相對(duì)于當(dāng)前用戶(hù)目錄(即System.getProperty("user.dir"))

System.out.println("dir = " + System.getProperty("user.dir"));


輸出:dir = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo


        File dotFile = new File(".");//表示當(dāng)前工程目錄,和當(dāng)前用戶(hù)目錄相同
        System.out.println("dotFile = " + dotFile.getCanonicalPath());
        
        File file1 = new File("..");//表示當(dāng)前工程目錄的上一級(jí)目錄
        System.out.println("file1 = " + file1.getCanonicalPath());
        
        File file2 = new File("/");//表示當(dāng)前工程目錄的最頂層目錄
        System.out.println("file2 = " + file2.getCanonicalPath());


輸出:

dotFile = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo
file1 = E:\hotyeWorkspace\webterminal
file2 = E:\


因此,

    File txtFile = new File("/bin/demo/demo1/1.txt");
        System.out.println("txtFile = " + txtFile);
        System.out.println("path = " + txtFile.getCanonicalPath());
        System.out.println("file = " + txtFile.isFile());


輸出:

txtFile = \bin\demo\demo1\1.txt
path = E:\bin\demo\demo1\1.txt
file = false


總感覺(jué)用File的路徑定位不靠譜而且難用,而且結(jié)合網(wǎng)上的資料,還是用classloader,即類(lèi)路徑來(lái)定位好些。


        System.out.println(Main.class.getResource(""));//相對(duì)于當(dāng)前文件的路徑,不包含自身(即Main類(lèi))
        System.out.println(Main.class.getResource("/"));//得到的是當(dāng)前的classpath的絕對(duì)URI路徑。
        System.out.println(Main.class.getClassLoader().getResource(""));//得到的是當(dāng)前的classpath的絕對(duì)URI路徑。相當(dāng)于 System.out.println(Main.class.getResource("/"));
        System.out.println(Main.class.getClassLoader().getResource("/"));//ClassLoader路徑不允許以/開(kāi)頭


輸出:

file:/E:/hotyeWorkspace/webterminal/GetResourceAsStreamDemo/bin/demo/
file:/E:/hotyeWorkspace/webterminal/GetResourceAsStreamDemo/bin/
file:/E:/hotyeWorkspace/webterminal/GetResourceAsStreamDemo/bin/
null


用classpath來(lái)定位1.txt

      方法1:  File txtFile = new File(Main.class.getClassLoader().getResource("demo/demo1/1.txt").getFile());
        System.out.println("txtFile = " + txtFile);
        System.out.println("path = " + txtFile.getCanonicalPath());
        System.out.println("file = " + txtFile.isFile());


輸出:

txtFile = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
path = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
file = true


      方法2:  File txtFile = new File(Main.class.getResource("/demo/demo1/1.txt").getFile());
        System.out.println("txtFile = " + txtFile);
        System.out.println("path = " + txtFile.getCanonicalPath());
        System.out.println("file = " + txtFile.isFile());


輸出:

txtFile = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
path = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
file = true

  方法3: File txtFile = new File(Main.class.getResource("demo1/1.txt").getFile());
        System.out.println("txtFile = " + txtFile);
        System.out.println("path = " + txtFile.getCanonicalPath());
        System.out.println("file = " + txtFile.isFile());


輸出:

txtFile = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
path = E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo\bin\demo\demo1\1.txt
file = true


getResourceAsStream 和 getResource類(lèi)似

InputStream in = Main.class.getResourceAsStream("demo1/1.txt");
        InputStream in1 = Main.class.getResourceAsStream("/demo/demo1/1.txt");
        InputStream in2 = Main.class.getClassLoader().getResourceAsStream("demo/demo1/1.txt");
        System.out.println("in = " + in);
        System.out.println("in1 = " + in1);
        System.out.println("in2 = " + in2);


輸出:

in = Java.io.BufferedInputStream@de6ced
in1 = java.io.BufferedInputStream@c17164
in2 = java.io.BufferedInputStream@1fb8ee3


這些代碼是寫(xiě)在Main類(lèi)中,IDE是eclipse,只在windows下測(cè)試過(guò)。

最近在學(xué)習(xí)classloader,也順便總結(jié)了下資源文件的定位,至于里面更深刻的原理還有待進(jìn)一步學(xué)習(xí)。








    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多