業(yè)精于勤,荒于嬉;行成于思,毀于隨。
If you want to obtain example.xml
, you can use the following 2 methods
AppTest.class.getResource("/example.xml");
AppTest.class.getClassLoader().getResource("example.xml");
Java
Resource path
path starts with
/
開頭則是指向磁盤根目錄。如/usr/local/bin/xxx
則是指向根目錄中/usr
directoryThe path starting with the file (folder) name is relative to the file in the
user.dir
directory. For example, the file actually pointed to byuser.dir
目錄下的文件。如example.xml
實際指向的文件是System.getProperty("user.dir") + "example.xml"
is theSystem.getProperty("user.dir") + "example.xml"
file
CLASSPATH resources have
class.getResource()
與classLoader.getResource()
two ways to obtain
If
Test.class.getResource()
資源是以/
開頭則指向CLASSPATH
root directoryIf
Test.class.getResource()
是以文件(包名)開頭則相對Test.class
file is located in packagefind resource
Test.class.getClassLoader().getResource()
是以CLASSPATH
Looking for resources in the root directorycan only start with a file (package name)
I also encountered your problem the day before yesterday. The solution is: File-Project Structure in the menu bar. Select Moudles in the opened interface. If you see the attached picture, you can use the configuration file under the normal solution resource.
Then add under the build tag of pml.xml file:
<resources>
<!--表示把java目錄下的有關xml文件,properties文件編譯/打包的時候放在resource目錄下-->
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>