博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java web 配置文件
阅读量:4190 次
发布时间:2019-05-26

本文共 2748 字,大约阅读时间需要 9 分钟。

 

 

 

1、配置文件放在src下,在普通class文件中读取

public String sayHello(String name)

    {
        String path = "config/jdbc_mysql.properties";
        Properties props = new Properties();
        String error = "";
        String context = "";
        try{
            //获得当前类加载的根目录
            context = AccountService.class.getClassLoader().getResource("").toURI().getPath();
            //将文件读入文件输入流,存入内存
            FileInputStream fis = new FileInputStream(new File(context + path));
            //加载文件流的属性
            props.load(fis);
        }catch(URISyntaxException e){
            error +="非法URI";
            e.printStackTrace();
        }catch(FileNotFoundException e){
            error +="找不到文件";
        }catch(IOException e){
            error +="配置文件加载异常";
        }
        String driver = props.getProperty("driverClassName");
        String user = props.getProperty("user");
        
        return "Hello World " + name +","+ context +","+ driver+"," + user+error;
    }

 

2、在servlet中读取

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // TODO Auto-generated method stub
        //response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //out.append("Served at: ").append(request.getContextPath());
        out.write("Served at: " + request.getContextPath());
        String propertiesPath = "mysql.properties";
        String propertiesPath2 = "WEB-INF/classes/config/jdbc_mysql.properties";
        //与上面只是少了WEB-INF/classes/
        String propertiesPath3 = "config/jdbc_mysql.properties";
        Properties props = new Properties();
        Properties props2 = new Properties();
        Properties props3 = new Properties();
        //获取Servlet上下文的绝对路径
        String servletContextPath = this.getServletContext().getRealPath("");
        try{
            //将文件读入输入流,存入内存
            FileInputStream fis = new FileInputStream(new File(servletContextPath + propertiesPath));
            //加载文件流的属性
            props.load(fis);
        }catch(FileNotFoundException e){
            out.write("<br>");
            out.write("找不到WebContent下的配置文件");
        }catch(IOException e){
            out.write("WebContent直接下属的配置文件加载失败");
        }
        
        try{
            FileInputStream fis2 = new FileInputStream(new File(servletContextPath +propertiesPath2));
            props2.load(fis2);
        }catch(FileNotFoundException e){
            out.write("<br>");
            out.write("找不到src路径下的配置文件<br/>");
        }
        
        out.write("<br/>Servlet上下文:" + servletContextPath+"<br/>");
        String haesoadb_user = props.getProperty("haesoadb_user");
        String driver = props2.getProperty("driverClassName");//driverClassName
        out.write("haesoadb_user:"+ haesoadb_user);
        out.write("<br/>");
        out.write("driver:"+driver);
        
        String classPath ="";    
        try{
             classPath = TestServlet.class.getClassLoader().getResource("").toURI().getPath();
             FileInputStream fis3 = new FileInputStream(new File(classPath +propertiesPath3));
             props3.load(fis3);
             
        }catch(Exception e){//URISyntaxException
            out.write("<br/>获取路径失败");
        }
        out.write("<br>通过class获取的绝对路径:" + classPath);
        String user = props3.getProperty("user_test");
        out.write("<br/>通过class读取配置文件的值:" +user);
    }

转载地址:http://igooi.baihongyu.com/

你可能感兴趣的文章
贾跃亭还没回国,退市的乐视网突然沸腾!连拉15个涨停?
查看>>
苹果春季发布会或推迟至4月 AirPods 3可能缺席
查看>>
苹果零售业逐步恢复:部分已允许顾客店内试用AirPods
查看>>
页面无法正常显示,淘宝又崩了!周五上班摸鱼被发现...
查看>>
一加9系列全网预约量破200万:3月24日见!
查看>>
realme X7 Pro至尊版确认:最便宜的曲面屏手机
查看>>
iPhone 12s渲染图曝光,刘海变小了!
查看>>
猎聘2020年Q4营收6.08亿元 同比增长47.6%
查看>>
数百台湾人把名字改成“鲑鱼”去吃免费日料,结果有人改不回来了
查看>>
2021款iPad Pro渲染图曝光:依旧采用双摄+激光雷达扫描仪
查看>>
华为P50首发麒麟9000L:5nm EUV工艺打造 配置有所缩减
查看>>
中兴通讯、江苏联通联合成立5G消息开放实验室
查看>>
iMac Pro全球下架 苹果或不再推出新品
查看>>
消息称荣耀7月发布年度旗舰 或用上旗舰级芯片骁龙 888
查看>>
被职场毒打的专科生
查看>>
中国移动订330万台Redmi K40系列 网友:怪不得抢不到
查看>>
全球芯片供应不足!苹果iPhone生产可能面临中断风险
查看>>
3成失眠者放下手机才能睡 说中你了吗?
查看>>
春招之下,909万应届生的去与留
查看>>
阿里云盘今日公测:无论用户是否付费,未来都不会限速
查看>>