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;
public class TestProperties {
public static void main(String[] args) throws IOException {
setProperties("test.properties");
readProperties("test.properties");
setXMLProperties("test.properties");
readXMLProperties("test.properties");
}
public static void readProperties(String filename) throws IOException {
Properties info = new Properties();
InputStream in = new FileInputStream(filename);
info.load(in);
String username = info.getProperty("username");
String password = info.getProperty("password");
System.out.printf("username:%s password:%s\n", username, password);
in.close();
}
public static void setProperties(String filename) throws IOException {
Properties info = new Properties();
OutputStream out = new FileOutputStream(filename);
info.setProperty("username", "zp");
info.setProperty("password", "1314");
info.store(out, "commt");
out.close();
}
public static void setXMLProperties(String filename) throws IOException {
Properties info = new Properties();
OutputStream out = new FileOutputStream(filename);
info.setProperty("username", "zp");
info.setProperty("password", "1314");
info.storeToXML(out, "commt");
out.close();
}
public static void readXMLProperties(String filename) throws IOException {
Properties info = new Properties();
InputStream in = new FileInputStream(filename);
info.loadFromXML(in);
String username = info.getProperty("username");
String password = info.getProperty("password");
System.out.printf("username:%s password:%s\n", username, password);
in.close();
}
}