opening an input stream to a file

D

Douglas

I'm learning struts at the moment and in one example the code for a business
class opens an input stream to a file using this code:

java.io.InputStream i =
this.getClass().getClassLoader().getResourceAsStream("UserFile");

The InputStream object is then used to load data from the file into a
java.util.Properties object.

My question is, why would someone open an input stream to a file this way?

Cheers.
 
F

Frank

I'm learning struts at the moment and in one example the code for a
business
class opens an input stream to a file using this code:

java.io.InputStream i =
this.getClass().getClassLoader().getResourceAsStream("UserFile");

The InputStream object is then used to load data from the file into a
java.util.Properties object.

My question is, why would someone open an input stream to a file this
way?

Cheers.

getResourceAsStream accounts not only that the file might be stored
naturally in the client's file system, but will also search within the
distributed .jar file for data files packaged with the program classes.

-HTH

Frank
 
D

Douglas

Ah, thanks.

But is there any diference between :

this.getClass().getClassLoader().getResourceAsStream("UserFile");

and

this.getClass().getResourceAsStream("UserFile");

I see both being used and see nothing in the documentation about using one
over the other.

Thnaks again.
 
O

Oscar kind

Douglas said:
But is there any diference between :

this.getClass().getClassLoader().getResourceAsStream("UserFile");

and

this.getClass().getResourceAsStream("UserFile");

I see both being used and see nothing in the documentation about using one
over the other.

Class#getResourceAsStream(String) does more than
ClassLoader#getResourceAsStream(String). From the API:

This method delegates the call to its class loader, after making
these changes to the resource name: if the resource name starts
with "/", it is unchanged; otherwise, the package name is
prepended to the resource name after converting "." to "/".
 
D

Douglas

OK, thanks.

Cheers.



Oscar kind said:
Class#getResourceAsStream(String) does more than
ClassLoader#getResourceAsStream(String). From the API:

This method delegates the call to its class loader, after making
these changes to the resource name: if the resource name starts
with "/", it is unchanged; otherwise, the package name is
prepended to the resource name after converting "." to "/".


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top