import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class test {
private String m_config = "config/text.properties";
private Properties prop = null;
private FileInputStream fis = null;
private FileOutputStream fos = null;
private String m_path ="";
public void init() {
prop = new Properties();
try {
fis = new FileInputStream(m_config);
prop.load(fis);
m_path = prop.getProperty("path");//这里可以从配置文件中读取属性path
System.out.println("m_path:"+m_path);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void setPath() {
m_path="2";
try {
//这里是把上面配置好的属性回传给配置文件
fos = new FileOutputStream(m_config);
prop.setProperty("path", m_path);
prop.store(fos, "");
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if(fos!=null){
fos.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
test t = new test();
t.init();
t.setPath();
}
}
配置文件是text.properties
路径如下
内容
运行结果
然后配置文件中数值改为2