java定位资源文件路径

来源:互联网 发布:如何做个淘宝客, 编辑:程序博客网 时间:2024/05/22 03:02

工程:GetResourceAsStreamDemo

磁盘路径:E:\hotyeWorkspace\webterminal\GetResourceAsStreamDemo

工程目录如下:



1.利用FIle定位:

File txtFile = new File("bin/demo/demo1/1.txt");//这里相当于File txtFile = new File("src/demo/demo1/1.txt"); 因为src的文件会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


这里,File里面的路径,假如不从"/"开始,就表示相对于当前用户目录(即System.getProperty("user.dir"))

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


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


        File dotFile = new File(".");//表示当前工程目录,和当前用户目录相同
        System.out.println("dotFile = " + dotFile.getCanonicalPath());
        
        File file1 = new File("..");//表示当前工程目录的上一级目录
        System.out.println("file1 = " + file1.getCanonicalPath());
        
        File file2 = new File("/");//表示当前工程目录的最顶层目录
        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的路径定位不靠谱而且难用,而且结合网上的资料,还是用classloader,即类路径来定位好些。


        System.out.println(Main.class.getResource(""));//相对于当前文件的路径,不包含自身(即Main类)
        System.out.println(Main.class.getResource("/"));//得到的是当前的classpath的绝对URI路径。
        System.out.println(Main.class.getClassLoader().getResource(""));//得到的是当前的classpath的绝对URI路径。相当于 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下测试过。

最近在学习classloader,也顺便总结了下资源文件的定位,至于里面更深刻的原理还有待进一步学习。








原创粉丝点击