File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.getResource(String) and Class.getResourceAsStream(String)

 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a question about the getResource(String) and getResourceAsStream(String) methods in the Class class. What exacty are the "resources"? Why is the return of getResource(String) a URL?
Thanks.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------
Hi,
I have a question about the getResource(String) and getResourceAsStream(String) methods in the Class class. What exacty are the "resources"? Why is the return of getResource(String) a URL?
Thanks.
--------------------------------------------
Hi,
Resource is some data like images, text... that can accessed by the class code.
It returns the URL for reading the resource.
I haven't tried these methods, so I don't have a practical example.
Vanitha
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do they search? The local drive? The Internet?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per the API:

Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.
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 "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.

So if you feed in a string "/myfile.txt" the file is searched for in just the path and classpath directories. If you feed in "myfile.txt" the package name is added and a search is done in exactly the same manner that class files are looked for, therefore the Path and classpath directories are searched.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic