package com.suyang.properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class TestProperties {
private final String PATH = "test.properties";
private static Properties props = new Properties();
static {
try {
props.load(new FileInputStream("test.properties"));//加载properties文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getProperty(String name) {
return props.getProperty(name); //读取属性
}
public void setProperty(String name, String value) {
props.setProperty(name, value); //设置属性
}
public void save(){
OutputStream fos = null;
try {
fos = new FileOutputStream(PATH);
props.store(fos, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}