【Java读取文件】Java读取properties、xml文件

Java读取properties、xml文件

1.读取properties文件

params.properties文件
com.year=2018
com.month=1
com.day=2

package properties;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
/**
 * 读取properties文件
 * @author wsz
 * @date 2018年1月2日
 */
public class Test {

	public static void main(String[] args) {
		readProperties1();
		readProperties2();
	}
	
	/**
	 * 需要文件与该类在同一目录下,否则无法找到文件
	 */
	private static void readProperties2() {
		try {
			InputStream is = Test.class.getResourceAsStream("params.properties"); 
			Properties prop = new Properties();
			prop.load(is);
			String year  = prop.getProperty("com.year");
			String month = prop.getProperty("com.month");
			String day   = prop.getProperty("com.day");
			System.out.println(year+"-"+month+"-"+day);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用Resourcebundle读取文件
	 */
	private static void readProperties1() {
		ResourceBundle bundle = ResourceBundle.getBundle("params");
		String year = bundle.getString("com.year");
		String month =  bundle.getString("com.month");
		String day = bundle.getString("com.day");
		System.out.println(year+"-"+month+"-"+day);
		//遍历时是乱序的
		Enumeration<String> keys = bundle.getKeys();
		for (Enumeration<String> e = keys; e.hasMoreElements() ; ) {
			String key   = e.nextElement();
			String value = bundle.getString(key);
			System.out.println(key+"-"+value);
		}
	}
}

2.读取xml文件

params.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<test>
	<value>
		<year>2018</year>
		<month>1</month>
		<day>2</day>
	</value>
	<value>
		<year>2018</year>
		<month>1</month>
		<day>3</day>
	</value>
</test>

package xml;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.helpers.DefaultHandler;
/**
 * 三种方式读取xml文件
 * @author wsz
 * @date 2018年1月2日
 */
public class Test extends DefaultHandler{

	public static void main(String[] args) throws Exception {
		readXml();
		readXml2();
		readXml3();
	}
	
	/**
	 * 第一种DOM方式
	 * w3cdom包
	 * @throws Exception
	 */
	private static void readXml() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(new File("params.xml"));
		NodeList list = doc.getElementsByTagName("value");
		for (int i = 0; i < list.getLength(); i++) {
			String year  = doc.getElementsByTagName("year").item(i).getFirstChild().getNodeValue();
			String month = doc.getElementsByTagName("month").item(i).getFirstChild().getNodeValue();
			String day   = doc.getElementsByTagName("day").item(i).getFirstChild().getNodeValue();
			System.out.println(year+"-"+month+"-"+day);
		}
	}

	/**
	 * 第2种DOM4J方式
	 * 需要引入domj包
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private static void readXml2() throws Exception {
		SAXReader reader       = new SAXReader(); 
		org.dom4j.Document doc = reader.read(new File("params.xml"));
		Element root = doc.getRootElement();
		Element e    = null;
		for (Iterator<Element> i= root.elementIterator("value") ; i.hasNext() ; ) {
			e = (Element) i.next();
			String year  = e.elementText("year");
			String month = e.elementText("month");
			String day   = e.elementText("day");
			System.out.println(year+"-"+month+"-"+day);
		}
	}
	
	/**
	 * 第3种JDOM方式
	 * 需要引入jdom包
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private static void readXml3() throws Exception {
		SAXBuilder builder = new SAXBuilder();   
		org.jdom.Document doc = builder.build(new File("params.xml"));
		org.jdom.Element  e   = doc.getRootElement();   
		List<org.jdom.Element> allChildren  = e.getChildren();   
		for (org.jdom.Element el : allChildren) {
			String year  = el.getChild("year").getText();
			String month = el.getChild("month").getText();
			String day   = el.getChild("day").getText();
			System.out.println(year+"-"+month+"-"+day);
		}
	}
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值