• Java中配置文件的三种配置位置及读取方式


    XML 和properties

    properties:

    1、存放于src根目录下

    //获取到同包下的资源文件,将其转换成流对象
    //InputStream is=	PropertiesDemo.class.getResourceAsStream("/db.properties");
    //需要专门的工具类来讲流中的数据
    Properties p=new Properties();
    p.load(is);
    System.out.println(p.getProperty("uname"));
    System.out.println(p.getProperty("upass"));

    2、与读取配置文件的类在同一包

    //InputStream is= PropertiesDemo.class.getResourceAsStream("db.properties");
    
    Properties p=new Properties();
    p.load(is);
    System.out.println(p.getProperty("uname"));
    System.out.println(p.getProperty("upass"));

    3、存放在WEB-INF(或其子目录下)

    //新建一个servlet类
    
    ServletContext context=request.getServletContext();
    InputStream is=context.getResourceAsStream("/WEB-INF/db.properties");
    Properties p=new Properties();
    p.load(is);
    System.out.println(p.getProperty("uname"));
    System.out.println(p.getProperty("upass"));
    
     
    

      

    4. dom4j+xpath解析xml文件

    InputStream is = XMLDemo.class.getResourceAsStream("students.xml");
    //	SAXReader saxReader = new SAXReader();
    //	Document doc = saxReader.read(is);
    //	System.out.println(doc.asXML());
    //	// xpath
    //	List<Element> stueles=doc.selectNodes("/students/student");
    //	for (Element element : stueles) {
    ////	if ("s002".equals(element.attributeValue("sid"))) {
    ////	System.out.println(element.asXML());
    //	Element name=(Element)element.selectSingleNode("name");
    //	System.out.println(name.asXML());
    //	System.out.println(name.getText());
    //	// }
    //	}
    //	Element stuele =(Element) doc.selectSingleNode("/students/student[@sid='s001']");
    //	System.out.println(stuele.asXML()); 
    

      

    package com.lingerqi.demo;
    
    import java.io.InputStream;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    /**
     * 解析指定路径下的资源文件(dom4j)
     * 
     * @author xyls
     *
     */
    public class XMLDemo {
    
    	public static void main(String[] args) throws Exception {
    
    		InputStream is = XMlDemo.class.getResourceAsStream("config.xml");
    		SAXReader reader = new SAXReader();
    		Document doc = reader.read(is);
    		List<Element> stueles = doc.selectNodes("/config/action");
    		for (Element element : stueles) {
    			Element forward = (Element) element.selectSingleNode("forward");
    			String type = element.attributeValue("type");
    			String path = element.attributeValue("path");
    			// if (path.equals("/loginAction")) {
    			// 2、获取第二个action中的type的值
    			// System.out.println(type);
    			// }
    			// 1、获取所有action中的type的值
    			// System.out.println(type);
    			// String fpath=forward.attributeValue("path");
    
    			// System.out.println(type);
    			if (element.attributeValue("path").equals("/loginAction")) {
    				List<Element> ford = (List<Element>) element.selectNodes("forward");
    				for (Element element2 : ford) {
    					String fpath = forward.attributeValue("path");
    					// 3、获取第二个action的所有forward的path
    					System.out.println(fpath);
    				}
    			}
    			if (path.equals("/loginAction")) {
    				// 4、获取第二个action的第二个forward的path
    				// System.out.println(fpath);
    			}
    		}
    		// if (path.equals("/loginAction")) {
    		// 4、获取第二个action的第二个forward的path
    		// System.out.println(fpath);
    		// }
    	}
    }
    

      

  • 相关阅读:
    最近一月研报推荐次数最多的最热股票
    【2019年07月22日】A股最便宜的股票
    【07月19日】指数估值排名
    北上资金近1周流入排行榜
    主要股东近3年净买入排名
    【07月16日】A股滚动市净率PB历史新低排名
    【07月15日】A股滚动市盈率PE最低排名
    最近3年股息率最高排名
    主要股东近3年净买入排名
    【07月09日】预分红股息率最高排名
  • 原文地址:https://www.cnblogs.com/omji0030/p/11000040.html
Copyright © 2020-2023  润新知