乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java定位資源文件路徑

       青_春 2016-06-22

      工程:GetResourceAsStreamDemo

      磁盤路徑: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的文件會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里面的路徑,假如不從"/"開始,就表示相對于當(dāng)前用戶目錄(即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)前用戶目錄相同
              System.out.println("dotFile = " + dotFile.getCanonicalPath());
              
              File file1 = new File("..");//表示當(dāng)前工程目錄的上一級目錄
              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


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


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


      輸出:

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


      用classpath來定位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類似

      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


      這些代碼是寫在Main類中,IDE是eclipse,只在windows下測試過。

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








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

        0條評論

        發(fā)表

        請遵守用戶 評論公約