my project directory structure
/src
/model
/Updatedata.java
/war
/vector
/basicvector.xml
which method i use to read basicvector.xml
from Updatedata
clas开发者_如何学Pythons
If you are writing a servlet, you can use
getServletContext().getResourceAsStream("/basicvector.xml")
or
getServletContext().getResourceAsStream("/vector/basicvector.xml")
(I can't quite tell from the question whether basicvector.xml is in the root of the .war or in a folder called vector
)
getClass().getResource("/"); but it return the path WEB-INF/class/
This is because while you are running, the WEB-INF/class/ folder is considered the current directory for CLASSPATH resolution. The call to getClass() is invoking the classloader used by the current class, and getResource() is asking that classloader to find a file. That is why "/" wasn't the root of the drive, it was the root/current dir of the classloader.
Knowing that, you could use relative paths to navigate backwards to where your XML file is.
Assuming that the structure of your code is as follows
/war/vector/basicvector.xml
/war/WEB-INF/class/
Then you could use:
getClass().getResource("/../../vector/basicvector.xml");
精彩评论