ServletContext对象读取资源文件

在ServletContext对象中有以下四个方法可以实现资源文件的读取

对于资源文件最好放到类路径下,这样可以用到类装载器去读取,当然也可以放到webRoot文件夹下,当资源文件放在webRoot文件夹下的时候可以采取在servlet中的ServletContext来读取资源文件

1、InputStream   getResourceAsStream(String path):传入资源路径返回输入流。

例:(servlet中读取)

    //读取配置文件的第一种方式(文件在webRoot文件夹下)

    注意:文件在webRoot文件夹下的话,就不能通过类装载器去读了,类装载器能读取的只是类目录下的文      

    private void test1() throws IOException {

       ServletContext context = this.getServletContext();

       //其中/代表web应用,表示web应用下的db.properties文件

       InputStream in = context.getResourceAsStream("/db.properties");

       Properties prop = new Properties();

       prop.load(in);

       //获取db.properties文件中的值

       String url = prop.getProperty("url");

       String username = prop.getProperty("username");

       String password = prop.getProperty("password");

    }

 

2、String  getRealPath(String path):传入虚拟路径返回实际路径。

    //读取配置文件的第二种方式(文件在webRoot文件夹下)

    private void test2() throws IOException {

       ServletContext context = this.getServletContext();

       //返回绝对路径

       String realpath = context.getRealPath("/db.properties"); 

       //与第一种方式的区别在于可以获取到操作文件名

       String filename = realpath.substring(realpath.lastIndexOf("\\")+1);

       System.out.println("当前读到的文件是:" + filename);

      

       FileInputStream in = new FileInputStream(realpath);

       Properties prop = new Properties();

       prop.load(in);

       String url = prop.getProperty("url");

       String username = prop.getProperty("username");

       String password = prop.getProperty("password");

    }

 

3、URL   getResource(String path):传入资源路径返回URL。

    //读取配置文件的第三种方式此方式用的少(文件在webRoot文件夹下)

    private void test3() throws IOException {

       ServletContext context = this.getServletContext();

       //此路径为web工程的resource文件夹下的db.properties

       URL url = context.getResource("/resource/db.properties");

       //调用url中的openStream方法获得输入流

       InputStream in = url.openStream();

       与之相同的模板读取代码

    }

 

4、Set  getResourcePaths(String path):传入路径返回路径下所以资源的集合。

                   此种方式获取集合后遍历再进行读取。

 

当文件在src下面该如何读取呢?其实src是在eclipse中,实际是/WEB-INF/classes这个路径

    //servlet中读取src下面的配置文件(文件在src中,也就是/WEB-INF/classes文件夹中)

    private void test5() throws IOException {

       InputStream in = this.getServletContext()

                         .getResourceAsStream("/WEB-INF/classes/db.properties");

       Properties prop = new Properties();

       prop.load(in);

       String url = prop.getProperty("url");

       String username = prop.getProperty("username");

       String password = prop.getProperty("password");

    }

5、当配置文件在dao中,不是servlet了,就得用到类装载器对象

    //以下代码在读文件时,当配置文件有改的时就读不到更新后的文件,由于类加载器只加载一次

    public void test1() throws IOException{

       //daoclass调用getClassLoader获得类装载器对象

       ClassLoader loader = StudentDao.class.getClassLoader();

       //再用类装载器获得输入流

       InputStream in = loader.getResourceAsStream("cn/itcast/dao/db.properties");

       Properties prop = new Properties();

       prop.load(in);

       String url = prop.getProperty("url");

       String username = prop.getProperty("username");

       String password = prop.getProperty("password");

    }

 

    //以下代码在读文件时,可以读到更新后的文件

    public void test2() throws IOException{

       ClassLoader loader = StudentDao.class.getClassLoader();

       //用类装载器获取url

       URL url = loader.getResource("cn/itcast/dao/db.properties");

       //获取文件的绝对路径

       String filepath = url.getPath();

       //再用传统方式去读取,这样就可以获取更改后的数据了

       FileInputStream in = new FileInputStream(filepath);

       Properties prop = new Properties();

       prop.load(in);

       String dburl = prop.getProperty("url");

       String username = prop.getProperty("username");

       String password = prop.getProperty("password");

    }

小知识:在实际开发中没有关系的数据的配置文件用properties,有关系就用XML

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值