zoukankan      html  css  js  c++  java
  • 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);
    		// }
    	}
    }
    

      

  • 相关阅读:
    强烈推荐:240多个jQuery插件【转】
    逆变与协变详解
    Mac入门 (二) 使用VMware Fusion虚拟机
    JQUERY UI DOWNLOAD
    几个常用Json组件的性能测试
    jQuery.extend 函数详解
    获取Portal中POWL程序的APPLID
    设计师和开发人员更快完成工作需求的20个惊人的jqury插件教程(上)
    Linux kernel中网络设备的管理
    mongodb修改器
  • 原文地址:https://www.cnblogs.com/omji0030/p/11000040.html
Copyright © 2011-2022 走看看