package com.lideng.work325;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
* 测试java.Utils的properties类
* @author Administrator
*
*/
public class Demo03 {
/**
* @param args
*/
private static Properties ps=null;
static{
ps=new Properties();
try {
ps.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(""));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
setXml();
}
public static void setXml(){
Properties ps=new Properties();
OutputStream os=null;
FileInputStream is=null;
try {
ps.setProperty("name", "LD");
ps.setProperty("password", "admin");
os = new FileOutputStream("c:/db.xml");
is=new FileInputStream("c:/db.xml");
ps.load(is);
ps.storeToXML(os, "");
System.out.println(ps.getProperty("name"));
System.out.println(ps.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取Xml文件信息
*/
public static void getXml(){
Properties ps=new Properties();
InputStream is;
try {
is = new FileInputStream(new File("c:/db.xml"));
ps.loadFromXML(is);
System.out.println(ps);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从类中加载properties文件
*/
public static void getPropertes(){
Properties ps=new Properties();
try {
ps.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
System.out.println(ps);
String property = ps.getProperty("url");
System.out.println(property);
} catch (IOException e) {
e.printStackTrace();
}
}
}