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的工具,再議,存着。

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