我在Eclipse中构建了一个动态Web项目MusicStore
,我想访问位于WebContent/_res/Songs.xml
的xml文件。我用来在常规Java类[not a servlet]
中访问它的方法是:
URL url = this.getClass().getClassLoader().getResource("/");
String filePath = url.getFile();
String webAppLoc=new File(filePath).getParentFile().getParent();
String resLoc=webAppLoc+"/_res/Songs.xml";
在我看来,这非常麻烦。有更好,更有效的方式吗?谢谢!
答案 0 :(得分:0)
您可以在此处尝试访问文件
public class Test {
public static void main(String[] args){
//This gives me path for the current working directory.
String fileName = System.getProperty("user.dir");
//This gives me path for the file that is residing in folder called "xml_tutorial".
File file = new File(fileName + "../../xml_tutorial/sample.xlsx" );
System.out.println(file.getCanonicalPath());
}
}