`

[Java]XML与Properties

阅读更多
package demo.xml;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class PropXmlDemo {

	public static void main(String[] args) throws IOException {
		propsToXml();
		xmlToProps();
	}
	
	static void propsToXml() throws IOException{
		Properties props = new Properties();
                //overwrite by the next my.email
		props.setProperty("my.email", "aaa.mail@163.com");
		props.setProperty("my.email.1", "aaa.mail@163.com");
		props.setProperty("my.email", "111aaa.mail@163.com");
		
		OutputStream out = new FileOutputStream("c:\\a.xml");
		props.storeToXML(out, "my.comments");
		out.close();
	}
	
	static void xmlToProps()throws IOException{
		Properties props = new Properties();
		//PropXmlDemo.class.getClassLoader().getResourceAsStream("a.xml");
		InputStream is = new FileInputStream("c:\\a.xml");
		props.loadFromXML(is);
		is.close();
		
		System.out.println(props.getProperty("my.email"));
		
		System.out.println(props.getProperty("my.email.1"));
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics