In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the role of getResourceAsStream in Java is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
First of all, there are several kinds of getResourceAsStream in Java:
1. Class.getResourceAsStream (String path): if path does not start with'/', it acquires resources from the package in which this class resides by default, and starts with'/ 'from the root of ClassPath. It only constructs an absolute path through path, and in the end, the resource is obtained by ClassLoader.
2. Class.getClassLoader.getResourceAsStream (String path): by default, it is obtained from the root of ClassPath. Path cannot start with'/'. In the end, ClassLoader acquires resources.
3. ServletContext. GetResourceAsStream (String path): resources are fetched from the WebAPP root directory by default. It doesn't matter whether path starts with'/ 'under Tomcat. Of course, this is related to the specific container implementation.
4. The application built-in object under Jsp is an implementation of ServletContext above.
Secondly, the uses of getResourceAsStream are roughly as follows:
First: the file to be loaded is in the same directory as the .class file. For example, there is a class me.class under com.x.y and a resource file myfile.xml.
Then, you should have the following code:
Me.class.getResourceAsStream ("myfile.xml")
Second, there is a class me.class under the subdirectory of the me.class directory, such as com.x.y, and the resource file myfile.xml under the com.x.y.file directory.
Then, you should have the following code:
Me.class.getResourceAsStream ("file/myfile.xml")
Third, it is neither in the me.class directory nor in the subdirectory, for example, there is a class me.class under com.x.y, and there is a resource file myfile.xml in the com.x.file directory.
Then, you should have the following code:
Me.class.getResourceAsStream ("/ com/x/file/myfile.xml")
To sum up, it's probably just two ways to write it.
First, there is a "/" in front of it.
"/" represents the root directory of the project, for example, the project is called myproject, and "/" represents myproject.
Me.class.getResourceAsStream ("/ com/x/file/myfile.xml")
Second, there is no "/" in front.
Represents the directory of the current class
Me.class.getResourceAsStream ("myfile.xml")
Me.class.getResourceAsStream ("file/myfile.xml")
The above content is what is the role of getResourceAsStream in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.