Background: Make a clear understanding of the absolute and relative paths of configuration file loading in a Java project!
1 Analysis Path
The getResourceAsStream function is often used in Java projects to get some configuration files, but how do you use this function correctly?
1.1 Familiarity with the root directory structure
getResourceAsStream (String path) The parameter of this function is a path, but how should this path be filled? The difficulty of using this function is also here.
In fact, just remember a key point,getresourceasstream This function to find the file starting point is the Java project after the compilation of the root directory , such as the general Maven project compiled after the root directory is target/classes this file, For example, the following directory tree is the distribution of files after a MAVEN project is compiled:
.
├──auth.server.properties
├──com
│├──winwill
││└──test
││├──testconstants.class
││├──testconstants$host.class
││├── testgetresourceasstream.class
││└──testguava.class
│└──xiaomi
│└──xmpush
│└──ios
│├──iospushtest.class
│├──uploadcertificate$1.class
│└──uploadcertificate.class
├──config
│└──config2.properties
├──config.properties
├──kafka-consumer.properties
├──kafka-producer.properties
├──meta-inf
│├──manifest. MF
│└──maven
│└──com.winwill.test
│└──test
│├──pom.properties
│└──pom.xml
├──redis.properties
└──zookeeper.properties
How do we get the two files of Config.properties and config2.properties in the Testgetresourceasstream.class class?
PS: To view the root structure of the project, you can view the classes file directory directly under the directory where the project is located. For example, the following project is not part of the MAVEN project
Project structure:
Post-compiled root directory structure:
1.2 using GetClass (). getResourceAsStream (String path)
Ps:path does not start with '/' by default from the package under which this class resides, and starts with '/' from the classpath root.
From the above directory tree can be seen, config This file path is/config.properties (/for the root directory), config2.properties the path of this file is/config/config2.properties,
So we can get these two files using the code below--Get the resources from the root
Packagecom.winwill.test; Importorg.junit.Test; Importjava.io.IOException; ImportJava.io.InputStream; /*** Created by Qifuguang on 14-9-28. */ Public classTestgetresourceasstream {@Test Public voidGetresourceclassandfileinsamepackage ()throwsIOException {//Get Config2.propertiesInputStream Config2 = This. GetClass (). getResourceAsStream ("/config/config2.properties"); //Get Config.propertiesInputStream config = This. GetClass (). getResourceAsStream ("/config.properties"); } }
The above code uses an absolute path starting from the root directory.
Can you use a relative path? Of course, take a look at the following code--get the resource from the package where the class is located
Packagecom.winwill.test; Importorg.junit.Test; Importjava.io.IOException; ImportJava.io.InputStream; /*** Created by Qifuguang on 14-9-28. */ Public classTestgetresourceasstream {@Test Public voidGetresourceclassandfileinsamepackage ()throwsIOException {//Get Config2.propertiesInputStream Config2 = This. GetClass (). getResourceAsStream (".. /.. /.. /config/config2.properties "); //Get Config.propertiesInputStream config = This. GetClass (). getResourceAsStream (".. /.. /.. /config.properties "); System.out.println (config+ " " +config2); } }
The two paths in the code are the relative paths of these two files relative to the Testgetresourceasstream.class file.
1.3 using GetClass (). getClassLoader (). getResourceAsStream (String path)
PS: The default is obtained from classpath root, path cannot start with '/', and eventually the resource is obtained by ClassLoader.
GetClass (). getClassLoader (). getResourceAsStream () The path that is used by default is the root directory of the class file , so use GetClass (). getClassLoader () . getResourceAsStream () to get the file can not be preceded by the path of/, so we can get these two files:
Packagecom.winwill.test; Importorg.junit.Test; Importjava.io.IOException; ImportJava.io.InputStream; /*** Created by Qifuguang on 14-9-28. */ Public classTestgetresourceasstream {@Test Public voidGetresourceclassandfileinsamepackage ()throwsIOException {//Get Config2.propertiesInputStream Config2 = This. GetClass (). getClassLoader (). getResourceAsStream ("Config/config2.properties"); //Get Config.propertiesInputStream config = This. GetClass (). getClassLoader (). getResourceAsStream ("Config.properties"); } }
1.4 WebApp root directory Fetch resources
ServletContext. getResourceAsStream (String path): The default is to fetch resources from the WebApp root directory, and whether or not Tomcat starts with '/' does not matter, of course, this is related to the specific container implementation.
The application built-in object under JSP is an implementation of the above ServletContext.
(go) How to use Java getResourceAsStream