小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java 三種讀取配置文件的方式

 真愛(ài)圖書(shū) 2012-09-27

方式一:采用ServletContext讀取,讀取配置文件的realpath,然后通過(guò)文件流讀取出來(lái)。因?yàn)槭怯?/SPAN>ServletContext讀取文件路徑,所以配置文件可以放入在web-infoclasses目錄中,也可以在應(yīng)用層級(jí)及web-info的目錄中。文件存放位置具體在eclipse工程中的表現(xiàn)是:可以放在src下面,也可放在web-infowebroot下面等。因?yàn)槭亲x取出路徑后,用文件流進(jìn)行讀取的,所以可以讀取任意的配置文件包括xmlproperties。缺點(diǎn):不能在servlet外面應(yīng)用讀取配置信息。

具體舉例如下:

//ServletContext.getRealPath(name)讀取路徑

    privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)

    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/開(kāi)頭具體的部署目錄路徑即可,如上面的/web-in…

       System.out.println(realPath);

       InputStreamReader reader = new InputStreamReader(newFileInputStream(realPath),"utf-8");

       Properties props = new Properties();

       props.load(reader); //load個(gè)人建議還是用Reader來(lái)讀,因?yàn)?/SPAN>reader體系中有個(gè)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個(gè)人建議還是用Reader來(lái)讀,因?yàn)?/SPAN>reader體系中有個(gè)InputStreamReader可以指定編碼

       jdbcConValue = props.getProperty("jdbc_con");

       System.out.println("second::"+jdbcConValue);

      

    }

方式二:采用ResourceBundle類(lèi)讀取配置信息,此種方式的優(yōu)點(diǎn)是:可以以完全限定類(lèi)名的方式加載資源后,直接的讀取出來(lái),且可以在非Web應(yīng)用中讀取資源文件。缺點(diǎn):只能加載類(lèi)classes下面的資源文件且只能讀取.properties文件。若資源文件的編碼是utf-8等其它的非is0-8859-1的編碼時(shí),需要將讀取出來(lái)的value先進(jìn)行getBytes(“iso-8859-1”)編碼得到原文,而用newString(bs[],”utf-8”)進(jìn)行解碼。

具體舉例如下:

//ResourceBundler讀取資源

    privatevoid test2(HttpServletRequest request, HttpServletResponseresponse)throws ServletException, IOException {

       //讀取src下面包的配置文件,似乎沒(méi)有什么好辦法可以讀取Bundle中是utf-8編碼的資源文件

       ResourceBundlerb = ResourceBundle.getBundle("com.test.servlet.jdbc_connection");

       String jdbcConValue = rb.getString("jdbc_con");

       System.out.println(jdbcConValue);//呵呵,搞定了。資源文件中是utf-8編碼的,但是ResourceBundler默認(rèn)是按iso-8859-1解碼的

       byte[] buf = jdbcConValue.getBytes("iso-8859-1");//所以讀取到了,要用iso-8859-1進(jìn)行解碼得到原本的byte后,再用utf-8進(jìn)行編碼

      

       System.out.println(new String(buf,"utf-8")); //這里再用utf-8進(jìn)行解碼就Ok.

      

    }

方式三:采用ClassLoader方式進(jìn)行讀取配置信息,此種方式的優(yōu)點(diǎn)是:可以在非Web應(yīng)用中讀取配置資源信息,可以讀取任意的資源文件信息。缺點(diǎn):只能加載類(lèi)classes下面的資源文件。

         具體舉例如下:

         //ClassLoader讀取資源

    privatevoid test3(HttpServletRequest request, HttpServletResponseresponse)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個(gè)人建議還是用Reader來(lái)讀,因?yàn)?/SPAN>reader體系中有個(gè)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來(lái)傳遞進(jìn)Properties---------------");

       InputStream in2 = cl.getResourceAsStream("com/test/servlet/jdbc_connection.properties");

       Reader reader = new InputStreamReader(in2,"utf-8"); //直接用轉(zhuǎn)換流來(lái)搞定

       Properties props2 = new Properties();

       props2.load(reader);

       jdbcConValue = props2.getProperty("jdbc_con");

       System.out.println(jdbcConValue);

    }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多