Z
zyng
I am sure the difference between the two have been discussed well: the first one is using the System classloader and the second one is using the classloader which has loaded this class, most likely a child of System classloader, according to what I have found on the web.
The part that is painful is that the first one, the file name cannot start with "/", while the second one must start with "/". I don't understand this.. If you can help me by shedding some light on this, I would greatly appreciate it.
For example, suppose I have a file "hello.txt" which locates in the directory "/abc/efg/hij/." And "/abc/efg" is on Java's classpath, so for the Java code to access "/abc/efg/hij/hello.txt" in the file system or access "hij/hello.txt" inside the JAR file:
//option 1:
InputStream is = ClassLoader.getSystemResourceAsStream("hij/hello.txt");
//option 2:
InputStream is = ResourceTools.class.getResourceAsStream("/hij/hello.txt");
The first one cannot start with "/" and the second one must start with "/".I found this is very confusing since the code between the two are already very similar looking.
Thank you very much.
The part that is painful is that the first one, the file name cannot start with "/", while the second one must start with "/". I don't understand this.. If you can help me by shedding some light on this, I would greatly appreciate it.
For example, suppose I have a file "hello.txt" which locates in the directory "/abc/efg/hij/." And "/abc/efg" is on Java's classpath, so for the Java code to access "/abc/efg/hij/hello.txt" in the file system or access "hij/hello.txt" inside the JAR file:
//option 1:
InputStream is = ClassLoader.getSystemResourceAsStream("hij/hello.txt");
//option 2:
InputStream is = ResourceTools.class.getResourceAsStream("/hij/hello.txt");
The first one cannot start with "/" and the second one must start with "/".I found this is very confusing since the code between the two are already very similar looking.
Thank you very much.