java中的资源路径resource path

java在操作文件的时候必然需要知道这个文件在哪里。每种文件就是一种资源,java获取资源的方式有两种:

1.通过java.io包中的类,例如File,其他类和File类似

2.通过Class或者ClassLoader

先看看File类:

An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependentpathname stringsto name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. Anabstract pathnamehas two components:

An optional system-dependentprefixstring, such as a disk-drive specifier,"/"for the UNIX root directory, or"\\\\"for a Microsoft Windows UNC pathname, andA sequence of zero or more stringnames.官方文档说File类是文件和目录名的一种抽象代表,不依赖于系统,注意理解这句话,在下面的测试代码中会有所体现

import java.io.File;import java.io.IOException;public class TestResourcePath {public static void main(String[] args) throws IOException {TestResourcePath t = new TestResourcePath();t.testFile();t.testClass();t.testClassLoader();}public void testFile(){File file=new File("dir/1.txt");//注意执行该句,即使这个文件不存在,这个文件也不会被创建,只是File类的对象被构造了,这个文件并不会产生//file.createNewFile();//File无法递归的创建文件及文件夹System.out.println(file.getAbsolutePath());File file2=new File("/dir/1.txt");//绝对路径System.out.println(file2.getAbsolutePath());File file3=new File("1.txt");try {file3.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//可行System.out.println(file3.getAbsolutePath());File file4=new File("2.txt");//相对路劲System.out.println(file4.getAbsolutePath());File file5=new File("");System.out.println(file5.getAbsolutePath());File file6=new File("RC/0.gif");System.out.println(file6.getAbsolutePath());File file7=new File("/RC/0.gif");//绝对路劲System.out.println(file7.getAbsolutePath());System.out.println(System.getProperty("user.dir"));System.out.println();}public void testClass(){System.out.println(this.getClass().getResource("RC/0.gif"));System.out.println(this.getClass().getResource("/RC/0.gif"));System.out.println(this.getClass().getResource("1.txt"));System.out.println(this.getClass().getResource("/1.txt"));System.out.println(this.getClass().getResource(""));System.out.println(System.getProperty("java.class.path"));System.out.println();}public void testClassLoader(){System.out.println(this.getClass().getClassLoader().getResource("RC/0.gif"));System.out.println(this.getClass().getClassLoader().getResource("/RC/0.gif"));System.out.println(this.getClass().getClassLoader().getResource("1.txt"));System.out.println(this.getClass().getClassLoader().getResource("/1.txt"));System.out.println(this.getClass().getResource(""));System.out.println(System.getProperty("java.class.path"));System.out.println();}}

下面是打印结果:

E:\Eclipse\javaStudyProjects\Reflect\dir\1.txtE:\dir\1.txtE:\Eclipse\javaStudyProjects\Reflect\1.txtE:\Eclipse\javaStudyProjects\Reflect\2.txtE:\Eclipse\javaStudyProjects\ReflectE:\Eclipse\javaStudyProjects\Reflect\RC\0.gifE:\RC\0.gifE:\Eclipse\javaStudyProjects\Reflect

这几个文件程序执行之前只有0.gif是存在的,执行之后只多了一个E:\Eclipse\javaStudyProjects\Reflect\1.txt;也即是说new File 时,即使这个文件不存在,这个文件也不会被创建,只是File类的对象被构造了,这个文件并不会产生,,印证了An abstract representation of file and directory pathnames.File类只是一个抽象代表,并不是正真的文件,要想这个文件创建,需要调用createNewFile()方法。如果文件已存在,createNewFile()方法返回假,不会再次创建。

关于pathname的写法:

加“/”前缀的表示的是绝对路径,看file2和file7

不加前缀的是相对路径,并且这个相对是相对于E:\Eclipse\javaStudyProjects\Reflect\这个路径,即user.dir

再来看看Class

打印结果:

file:/E:/Eclipse/javaStudyProjects/Reflect/bin/RC/0.giffile:/E:/Eclipse/javaStudyProjects/Reflect/bin/RC/0.gifnullnullfile:/E:/Eclipse/javaStudyProjects/Reflect/bin/

E:\Eclipse\javaStudyProjects\Reflect\bin

可以看出这个里面的前缀“/”不在起作用,因为/被过滤掉了,最后的结果都相当于相对路径,并且这个相对路径是E:/Eclipse/javaStudyProjects/Reflect/bin/,即java.class.path;也即是当前运行的类所在包的顶层包所在路径;看看Class.getResource()源码

少一点预设的期待,那份对人的关怀会更自在

java中的资源路径resource path

相关文章:

你感兴趣的文章:

标签云: