Please help me understand Class.getResourceAsStream()

W

www

Hi,

My Java program needs to do a lot of reading from text files or writing
to text files. I always provide the absolute path to those files. Things
are working.

But, I have heard that Class.getResourceAsStream() can do things more
intelligent: after specifying the path, the Java will search for the
file along the path to find the file with name matches what I want. This
will be useful, since my files have unique file names.

Is my understanding correct? I don't see many people using it and few
examples out there to help me start it. Could you give me some help?
Thank you.
 
M

Manish Pandit

Hi,

My Java program needs to do a lot of reading from text files or writing
to text files. I always provide the absolute path to those files. Things
are working.

But, I have heard that Class.getResourceAsStream() can do things more
intelligent: after specifying the path, the Java will search for the
file along the path to find the file with name matches what I want. This
will be useful, since my files have unique file names.

Is my understanding correct? I don't see many people using it and few
examples out there to help me start it. Could you give me some help?
Thank you.

Yes, your understanding is correct that the vm will look for the
resource, and return you an input stream for it. However, the resource
will not be searched in $PATH if that is what you mean by "path". You
will have to ensure that the resource or the jar file containing the
resource is in the *classpath*.

-cheers,
Manish
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top