A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© SyouRai_Tsk 高级黑马   /  2014-3-11 15:44  /  664 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

先看一下我把那些配置文件放在Web资源的哪些位置

package cn.itcast.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//其他读取资源文件的方式
public class ServletDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test7();
      
    }
    //利用ServletContext读取a1.properties
    private void test1() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/a1.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a2.properties
    private void test2() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/a2.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a3.properties
    private void test3() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/cn/itcast/resources/a3.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //------------------------------------------------------------------
    //利用ResourceBundle读取配置文件a2.properties
    private void test4() throws IOException, FileNotFoundException {
        //ResourceBundle是Java自带的一个方法,不能new出来,只能调用它的内部静态方法
        ResourceBundle rb = ResourceBundle.getBundle("a2");//基名(也就说扩展名前面那个名字  E.a3.properties  基名也就是a3)
        String value = rb.getString("username");
        System.out.println(value);
    }
    //利用ResourceBundle读取配置文件a3.properties
    private void test5() throws IOException, FileNotFoundException {
        ResourceBundle rb = ResourceBundle.getBundle("cn.itcast.resources.a3");//基名
        String value = rb.getString("username");
        System.out.println(value);
    }
    //-----------------------------------------------------------------------------------
   
    /*
         接下来,利用类加载器读取配置文件a2.properties
         也就是调用ClassLoader
         下面是最基础的使用,可是这里有一个缺点,那就是如果你在程序发布以后,如果修改了properties配置文件,它读取时不改变数据的
         这也是网络上经常有人会问,因为如果你知道Java运行机制的话,就知道数据一旦编译之后就不会改变
        
         所以,我会在test7()里面做点小小的改动就修改了这个Bug
         顺便说一声,如果使用ServletContext去获取文件路径的话,就不会产生这个Bug,但是,ServletContext只能在Servlet中使用
    如果你是在Dao中想获取配置文件的信息呢,虽然可以把ServletContext的值去传递,但是在开发中不会去使用,因为你WEB层嵌入到了Dao层,数据不安全
        
     */
    private void test6() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        InputStream in = cl.getResourceAsStream("a2.properties");
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
    /*
         test7就是为了解决test6的问题才弄出来
         比如你一个Dao想访问本地的配置文件,可是因为不是ServletContext,那你就不能调用那些获取路径的方法
         这时候我们应该怎么办
         还有一个问题也摆在面前:那就是如果你在程序发布以后,如果修改了properties配置文件,它读取时不改变数据的,这又该怎么解决
         解决方法是:我们通过类加载器获取文件路径,然后用传统方法去读取文件
     */
    private void test7() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        /*
            InputStream in = cl.getResourceAsStream("a2.properties");
           
            把这个换掉
           
        */
        URL url=cl.getResource("a2.properties");
        InputStream in=url.openStream();
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

psb.jpg (63.19 KB, 下载次数: 0)

psb.jpg

1 个回复

倒序浏览
刚好这一块我还不太清楚
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马