java读取配置文件

好多东西都配置在配置文件里,好多情况都要从配置文件里取值,那么不同类型的配置文件该怎么解析呢?

1.在servlet里面获取web.xml的属性配置值。

public void init(ServletConfig config) throws ServletException {
		//从servlet中获得初始化的参数值
		//获得所有的参数名
		Enumeration enums=config.getInitParameterNames();
		while(enums.hasMoreElements()){
			String name=(String)enums.nextElement();
			String value=config.getInitParameter(name);
			System.out.println(value);
		}
	}

2.获取.properties文件里配置的属性值(比如说数据库连接参数的配置,log4j日志参数的配置)。

public static String getRelativepath(String keyName) {
		Properties propertie = new Properties();
		InputStream inputStream = null;
		String keyValue = null;
		try {
			//读取属性文件
			inputStream = FilePath.class.getClassLoader().getResourceAsStream("配置文件路径/配置文件名.properties");
			if(null != inputStream){
				propertie.load(inputStream);
				keyValue = propertie.getProperty(keyName);
				inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return keyValue;
	}

3.获取xml配置文件配置的某属性的值(针对复杂些的配置,自己自定义的一些xml文件按固定格式解析,生成)。

(a).读取xml的数据

利用封装好的解析xml的工具。

(b).程序生成xml文件

利用封装好的解析xml的工具。

public class CreateDocument {
 
	public static void main(String[] args) {
		// 调用xml生成方法
		createDocument(new File("E:\\person.xml"));
	}
	
	public static void createDocument(File file){
		try {
			// 初始化xml解析工厂
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			
			// 创建DocumentBuilder
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			// 创建Document
			Document doc = builder.newDocument();
			// standalone用来表示该文件是否呼叫其它外部的文件。若值是 ”yes” 表示没有呼叫外部文件
			doc.setXmlStandalone(true);
			
			// 创建一个根节点
			// 说明: doc.createElement("元素名")、element.setAttribute("属性名","属性值")、element.setTextContent("标签间内容")
			Element element = doc.createElement("root");
			element.setAttribute("attr", "root");
			
			// 创建根节点第一个子节点
			Element elementChildOne = doc.createElement("person");
			elementChildOne.setAttribute("attr", "personOne");
			element.appendChild(elementChildOne);
			
			// 第一个子节点的第一个子节点
			Element childOneOne = doc.createElement("people");
			childOneOne.setAttribute("attr", "peopleOne");
			childOneOne.setTextContent("attr peopleOne");
			elementChildOne.appendChild(childOneOne);
			
			// 第一个子节点的第二个子节点
			Element childOneTwo = doc.createElement("people");
			childOneTwo.setAttribute("attr", "peopleTwo");
			childOneTwo.setTextContent("attr peopleTwo");
			elementChildOne.appendChild(childOneTwo);
			
			// 创建根节点第二个子节点
			Element elementChildTwo = doc.createElement("person");
			elementChildTwo.setAttribute("attr", "personTwo");
			element.appendChild(elementChildTwo);
			
			// 第二个子节点的第一个子节点
			Element childTwoOne = doc.createElement("people");
			childTwoOne.setAttribute("attr", "peopleOne");
			childTwoOne.setTextContent("attr peopleOne");
			elementChildTwo.appendChild(childTwoOne);
			
			// 第二个子节点的第二个子节点
			Element childTwoTwo = doc.createElement("people");
			childTwoTwo.setAttribute("attr", "peopleTwo");
			childTwoTwo.setTextContent("attr peopleTwo");
			elementChildTwo.appendChild(childTwoTwo);
			
			// 添加根节点
			doc.appendChild(element);
			
			// 把xml内容输出到具体的文件中
			TransformerFactory formerFactory=TransformerFactory.newInstance();
			Transformer transformer=formerFactory.newTransformer();
			// 换行
			transformer.setOutputProperty(OutputKeys.INDENT, "YES");
			// 文档字符编码
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
			
			// 可随意指定文件的后缀,效果一样,但xml比较好解析,比如: E:\\person.txt等
			transformer.transform(new DOMSource(doc),new StreamResult(file));
			
			System.out.println("xml CreateDocument success!");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
}

解析xml的工具,再议,存着。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章