Usage of getResourceAsStream in Java

tags: getResourceAsStream

Record it for easy review. . .

First, there are several types of getResourceAsStream in Java: 
1. Class.getResourceAsStream(String path) : Path does not start with '/', the default is to take resources from the package where this class is located, starting with '/' is obtained from the ClassPath root. It just constructs an absolute path through the path, and finally gets resources from the ClassLoader.

2. Class.getClassLoader.getResourceAsStream(String path) :By default, it is obtained from the ClassPath root. The path cannot start with '/', and finally the resource is obtained by the ClassLoader.

3. ServletContext. getResourceAsStream(String path):By default, resources are taken from the root directory of the WebAPP. It doesn't matter if the path under Tomcat starts with '/'. Of course, this is related to the concrete container implementation.

4. The application built-in object under Jsp is an implementation of the above ServletContext.

Second, the usage of getResourceAsStream is roughly the following: 

the first: The file to be loaded and the .class file are in the same directory, for example: me.class under com.x.y and resource file myfile.xml

Then, there should be the following code:

 

me.class.getResourceAsStream("myfile.xml"); 



second:In the subdirectory of the me.class directory, for example: me.class under com.x.y, and the resource file myfile.xml in the com.x.y.file directory.

Then, there should be the following code:

 

me.class.getResourceAsStream("file/myfile.xml"); 


third:Not in the me.class directory, nor in the subdirectory, for example: me.class under com.x.y, and the resource file myfile.xml in the com.x.file directory.

Then, there should be the following code:
 

me.class.getResourceAsStream("/com/x/file/myfile.xml"); 


To sum up, it may be just two ways of writing

the first:In front of " / "

“ / ” represents the root directory of the project, for example, the project name is myproject, and “ / ” stands for myproject

me.class.getResourceAsStream("/com/x/file/myfile.xml"); 

second:No " / " in front

represents the directory of the current class

 

me.class.getResourceAsStream("myfile.xml"); 

me.class.getResourceAsStream("file/myfile.xml"); 

Finally, my own understanding: 
The file path read by getResourceAsStream is only limited to the project's source folder, including the project src root directory, and any location inside the class package, but if the configuration file path is in addition to the source This method is not used when you are in a folder other than a folder. (It can be simply recorded, this is just reading the files in the project. If the files in the C disk of Windows C cannot be read, there is something wrong to correct it...)

original:

A small example I wrote:https://github.com/ToBeAMonk/MyTest/blob/master/src/com/gty/test/GetResource.java

 

Intelligent Recommendation

About the usage of class.getClassLoader().getResourceAsStream() and class.getResourceAsStream()

1.class.getResourceAsStream() ①. Without ‘/’, start searching directly under the path of the package, which is equivalent to adding a sentence /package name/path before the path that has b...

getResourceAsStream

directory structure src:reflect/ReflectTest2.java reflect/resource/config.properties The class's getResourceAsStream is relative to the class loader directory path of the current class. The current pa...

[Java] The difference between getClass().getClassLoader().getResourceAsStream() and getClass().getResourceAsStream()

Code Output in conclusion Load the file with the specified name from the current package getClass().getResourceAsStream("config.properties"); this.getClass().getResourceAsStream("config...

Springboot reads the usage of configuration file getResourceAsStream

First of all, there are the following types of getResourceAsStream in Java:  1. Class.getResourceAsStream(String path): If the path does not start with ‘/’, the default is to fetch re...

Java uses getResource and getResourceAsStream to get the path of the file

Java uses getResource and getResourceAsStream to get the path of the file is based on the following project: the src directory is set to source root and the resources directory is set to resources roo...

More Recommendation

Java uses getClass (). GetResourceAsStream () method access to resources

Prior to want to get a resource file to do some processing, use getClass (). GetResourceAsStream () has been unable to get the file. Specific usage.    Specific file and location of the code is in src...

The getResource () and getResourceAsStream () methods of ClassLoader and Class in Java

Although there are numerous differences between the getResource () and getResourceAsStream () methods of ClassLoader and Class on the Internet, but I also encountered this problem, so I recorded it. &...

GetresourceASSTREAM in Java Return to NULL problem processing

GetresourceASSTREAM in Java Return to NULL problem processing: The main reason for this problem is the setting of the source: ​ In our commonly used IDEA, the input stream returned with GetResourceASS...

Java| Java two methods to load resource files getResource and getResourceAsStream

Article Directory 1. Class's getResource and getResourceAsStream 2. ClassLoad's getResource and getResourceAsStream 3. Suggestions Project source file structure The structure of the project after comp...

getResourceAsStream and getClassLoader (). getResourceAsStream

Directly on the map, to facilitate comparison I do not understand even a path in which the above configuration, oh, on the map, even though some of my colleagues in the eyes of eclise have become anti...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top