個人專題目錄
方式一:
采用ServletContext讀取民晒,讀取配置文件的realpath进栽,然后通過文件流讀取出來说莫。因為是用ServletContext讀取文件路徑镣丑,所以配置文件可以放入在web-info的classes目錄中奢入,也可以在應(yīng)用層級及web-info的目錄中展辞。文件存放位置具體在eclipse工程中的表現(xiàn)是:可以放在src下面,也可放在web-info及webroot下面等划提。因為是讀取出路徑后枫弟,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties鹏往。缺點:不能在servlet外面應(yīng)用讀取配置信息淡诗。
具體舉例如下:
//ServletContext.getRealPath(name)讀取路徑
private void test1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //讀取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServletContext()相當(dāng)于http://localhost/demo05
//所以后面的path只需要以應(yīng)用demo/開頭具體的部署目錄路徑即可,如上面的/web-in…
System.out.println(realPath);
InputStreamReader reader = new InputStreamReader(new FileInputStream(realPath), "utf-8");
Properties props = new Properties();
props.load(reader); //load個人建議還是用Reader來讀伊履,因為reader體系中有個InputStreamReader可以指定編碼
String jdbcConValue = props.getProperty("jdbc_con");
System.out.println(jdbcConValue);
System.out.println("加載src包下的資源------------------------");
path = "/WEB-INF/classes/com/test/servlet/jdbc_connection.properties"; //讀取WEB-INF中的配置文件
realPath = getServletContext().getRealPath(path);
System.out.println(realPath);
reader = new InputStreamReader(new FileInputStream(realPath), "utf-8");
props.load(reader); //load個人建議還是用Reader來讀韩容,因為reader體系中有個InputStreamReader可以指定編碼
jdbcConValue = props.getProperty("jdbc_con");
System.out.println("second::" + jdbcConValue);
}
方式二:
采用ResourceBundle類讀取配置信息,此種方式的優(yōu)點是:可以以完全限定類名的方式加載資源后唐瀑,直接的讀取出來群凶,且可以在非Web應(yīng)用中讀取資源文件。缺點:只能加載類classes下面的資源文件且只能讀取.properties文件哄辣。若資源文件的編碼是utf-8等其它的非is0-8859-1的編碼時请梢,需要將讀取出來的value先進行g(shù)etBytes(“iso-8859-1”)編碼得到原文,而用newString(bs[],”utf-8”)進行解碼力穗。
具體舉例如下:
//ResourceBundler讀取資源
private void test2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//讀取src下面包的配置文件毅弧,似乎沒有什么好辦法可以讀取Bundle中是utf-8編碼的資源文件
ResourceBundle rb = ResourceBundle.getBundle("com.test.servlet.jdbc_connection");
String jdbcConValue = rb.getString("jdbc_con");
System.out.println(jdbcConValue);//呵呵,搞定了当窗。資源文件中是utf-8編碼的夹纫,但是ResourceBundler默認是按iso-8859-1解碼的
byte[] buf = jdbcConValue.getBytes("iso-8859-1");//所以讀取到了峡谊,要用iso-8859-1進行解碼得到原本的byte后患久,再用utf-8進行編碼
System.out.println(new String(buf, "utf-8")); //這里再用utf-8進行解碼就Ok另萤。.
}
方式三:
采用ClassLoader方式進行讀取配置信息,此種方式的優(yōu)點是:可以在非Web應(yīng)用中讀取配置資源信息嘶朱,可以讀取任意的資源文件信息蛾坯。缺點:只能加載類classes下面的資源文件。
具體舉例如下:
//ClassLoader讀取資源
private void test3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ClassLoader cl = ServletReadConfig.class.getClassLoader();
InputStream in = cl.getResourceAsStream("com/test/servlet/jdbc_connection.properties");
Properties props = new Properties();
props.load(in); //load個人建議還是用Reader來讀疏遏,因為reader體系中有個InputStreamReader可以指定編碼
String jdbcConValue = props.getProperty("jdbc_con");
byte[] resoucreBytes = jdbcConValue.getBytes("iso-8859-1");
System.out.println(new String(resoucreBytes, "utf-8"));
System.out.println("----------ClassLoader讀取資源讀取脉课,用Reader來傳遞進Properties---------------");
InputStream in2 = cl.getResourceAsStream("com/test/servlet/jdbc_connection.properties");
Reader reader = new InputStreamReader(in2, "utf-8"); //直接用轉(zhuǎn)換流來搞定
Properties props2 = new Properties();
props2.load(reader);
jdbcConValue = props2.getProperty("jdbc_con");
System.out.println(jdbcConValue);
}