Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20077

ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream()

Newsgroups comp.lang.java.programmer
Date 2012-12-03 11:29 -0800
Message-ID <3c227741-7b7f-4cf8-b188-ce5268894031@googlegroups.com> (permalink)
Subject ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream()
From zyng <xsli2@yahoo.com>

Show all headers | View raw


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 class loader 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.

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() zyng <xsli2@yahoo.com> - 2012-12-03 11:29 -0800
  Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() markspace <-@.> - 2012-12-03 12:07 -0800
    Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-03 14:04 -0800
      Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() markspace <-@.> - 2012-12-03 15:25 -0800
        Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-03 20:37 -0800
  Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() zyng <xsli2@yahoo.com> - 2012-12-04 05:40 -0800
    Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-04 12:57 -0800

csiph-web