java 三种读取配置文件的方式

方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-infoclasses目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-infowebroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xmlproperties。缺点:不能在servlet外面应用读取配置信息。

具体举例如下:

//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()相当于http://localhost/demo05

//所以后面的path只需要以应用demo/开头具体的部署目录路径即可,如上面的/web-in…

       System.out.println(realPath);

       InputStreamReader reader = new InputStreamReader(newFileInputStream(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类读取配置信息,此种方式的优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。若资源文件的编码是utf-8等其它的非is0-8859-1的编码时,需要将读取出来的value先进行getBytes(“iso-8859-1”)编码得到原文,而用newString(bs[],”utf-8”)进行解码。

具体举例如下:

//ResourceBundler读取资源

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

       //读取src下面包的配置文件,似乎没有什么好办法可以读取Bundle中是utf-8编码的资源文件

       ResourceBundlerb = 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方式进行读取配置信息,此种方式的优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息。缺点:只能加载类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个人建议还是用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"); //直接用转换流来搞定

       Properties props2 = new Properties();

       props2.load(reader);

       jdbcConValue = props2.getProperty("jdbc_con");

       System.out.println(jdbcConValue);

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值