2006年12月29日
參考資料:
java中相對(duì)路徑,絕對(duì)路徑問(wèn)題總結(jié)(verygood)
http://www.aygfsteel.com/efine66/archive/2006/12/12/87097.html
?如何在java中使用相對(duì)路徑?
http://dev.csdn.net/develop/article/39/39681.shtm
通過(guò)虛擬路徑或相對(duì)路徑讀取一個(gè)xml文件,避免硬編碼
http://www.128kj.com/article/article5/7D66983DD9DD98A7753422A3A527FB6D.htm?id=438
java使用相對(duì)路徑讀取xml文件:
一、xml文件一般的存放位置有三個(gè):
1.放在WEB-INF下;
2.xml文件放在/WEB-INF/classes目錄下或classpath的jar包中;
3.放在與解析它的java類同一個(gè)包中,不一定是classpath;
二、相對(duì)應(yīng)的兩種使用相對(duì)路徑的讀取方法:
方法一:(未驗(yàn)證)
將xml文件放在WEB-INF目錄下,然后
程序代碼:
InputStream is=getServletContext().getResourceAsStream( "/WEB-INF/xmlfile.xml" );
方法二:將xml文件放在/WEB-INF/classes目錄下或classpath的jar包中,則可以使用ClassLoader的靜態(tài)方法getSystemResourceAsStream(String s)讀取;
程序代碼:
String s_xmlpath="com\xml\hotspot.xml";
InputStream in=ClassLoader.getSystemResourceAsStream(s_xmlpath);
方法三:xml在隨意某個(gè)包路徑下:
String s_xmlpath="com\xml\hotspot.xml";
ClassLoader classLoader=HotspotXmlParser.class.getClassLoader();
InputStream in=classLoader.getResourceAsStream(s_xmlpath);
?