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);
		// }
	}
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM