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


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

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

Received by 10.66.84.202 with SMTP id b10mr2492709paz.43.1354562944223; Mon, 03 Dec 2012 11:29:04 -0800 (PST)
Received by 10.50.34.167 with SMTP id a7mr98068igj.5.1354562944005; Mon, 03 Dec 2012 11:29:04 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!169.228.66.3.MISMATCH!ihnp4.UCSD.Edu!newsfeed.news.ucla.edu!usenet.stanford.edu!kr7no1348009pbb.0!news-out.google.com!s9ni24965pbb.0!nntp.google.com!kt20no2198466pbb.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Mon, 3 Dec 2012 11:29:03 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=140.90.91.121; posting-account=oo44OQoAAABMDvaRlaO3SeQ5MVbxqLdH
NNTP-Posting-Host 140.90.91.121
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <3c227741-7b7f-4cf8-b188-ce5268894031@googlegroups.com> (permalink)
Subject ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream()
From zyng <xsli2@yahoo.com>
Injection-Date Mon, 03 Dec 2012 19:29:04 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.java.programmer:20077

Show key headers only | 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