Search code examples
javaservletswarweb-deploymentdom4j

How to read an internal file in a web project


All, Forgive me I am still learning the Java development right row.

Say we have the structure of web project like below.

The src and config is under the Java Resources folder of web project.

src
 ... 
 |- a.b.c.package
    |-test.java
 ...
config
 |-1.xml
 |-2.xml
   ...
configfolder
 |-1.properties  
 |-2.properties
testfolder
 |-test.properties 

I want to know if I use the dom4j. How to read the xml file(1.xml) within the test.java. Thanks.

My Experiment

In the test.java. I found I can succeed to use the class.getClassLoader().getResourceAsStream("test.properties") to load test.properties in any folders or sub folder of src.

Does it mean getResourceAsStream can search the specified file in any of the folders of project recursively? I just can't understand it how it works. Thanks.


Solution

  • getResourceAsStream() method searches for resources with classpath as root. I suppose in your case 'testfolder' is source folder. I you would have your 1.properties in 'testfolder/mypath/1.properties' then you need to specify getResourceAsStream("mypath/1.properties")