ReadFileUtils

title: UtilsClass_ReadFileUtils
date: 2015-12-28 18:30:59
categories: UtilsClass_Resource
tags: Utils


xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003

读取文件

读取方式一:使用类的加载器

public class ReadFileServlet extends HttpServlet {
    public void ReadFile() {
            InputStream is = ReadFileServlet.class.getClassLoader().getResourceAsStream("ReadFile.properties");
            Properties prop = new Properties();
            prop.load(is);

            String driverClass = prop.getProperty("driverClass");
            String url = prop.getProperty("url");
            String username = prop.getProperty("username");
            String password = prop.getProperty("password");

            System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
        }
}

读取方式二:使用this.getServletContext();

public class ReadFileServlet1 extends HttpServlet {
    public void ReadFile() {
        //获取全局域变量ServletContext
        ServletContext context = this.getServletContext();

        //使用全局变量获取输入流
        InputStream is = context.getResourceAsStream("/WEB-INF/classes/ReadFile.properties");
        Properties prop = new Properties();
        prop.load(is);

        String driverClass = prop.getProperty("driverClass");
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");

        System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
        }
}

读取方式三:使用context.getRealPath();

public class ReadFileServlet2 extends HttpServlet {
    public void ReadFile() {
        //获得ServletContext全局域对象
        ServletContext context = this.getServletContext();
        //得到文件在磁盘的绝对路径
        String realPath = context.getRealPath("/WEB-INF/classes/ReadFile.properties");

        //使用字节输入流加绝对路径读取文件
        InputStream is = new FileInputStream(realPath);

        Properties prop = new Properties();
        prop.load(is);

        String driverClass = prop.getProperty("driverClass");
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");

        System.out.println(driverClass + ":" + url + ":" + username + ":" + password);
        }
}

使用全局变量来读取配置文件中的值

配置文件信息

 <context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>
  </context-param>
  <context-param>
    <param-name>password</param-name>
    <param-value>123</param-value>
  </context-param>
  <context-param>
    <param-name>111</param-name>
    <param-value>222</param-value>
  </context-param>

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = this.getServletContext().getInitParameter("username");
        String password = this.getServletContext().getInitParameter("password");
        System.out.println(username + password);

        //获得枚举类型
        Enumeration<String> e = this.getServletContext().getInitParameterNames();

        //遍历枚举对象
        while(e.hasMoreElements()){
            String name = e.nextElement();
            String value = this.getServletContext().getInitParameter(name);
            System.out.println(name + "\t" + value);
        }
    }

使用全局变量来读取文件在网络上的类型

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得文件MIME的类型(主要是判断文件在网络上的类型,用于上传和下载)
        String type = this.getServletContext().getMimeType("aa.html");
        System.out.println(type);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xlecho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值