属性(Properties)文件的写入
package properties;
import java.io.*;
import java.util.Properties;
public class PropertiesLoad {
public static void main(String[] args) {
Properties pro=new Properties();
InputStream input=null;
try {
input=new FileInputStream("c:"+File.separator+"test.txt");
pro.load(input); // 从字节流中读取数据加载到Properties中
System.out.println("是否为VIP:"+pro.getProperty("isVIP"));
System.out.println("上次更新时间:"+pro.getProperty("updatetime"));
System.out.println("应用模式:"+pro.getProperty("mode"));
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package properties;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class PropertiesStore {
public static void main(String[] args) {
Properties pro=new Properties();
pro.setProperty("mode","OPEN");
pro.setProperty("isVIP","no");
pro.setProperty("updatetime","20180101");
OutputStream out=null;
try {
out=new FileOutputStream("c:"+File.separator+"test.txt");
pro.store(out,"this is my default information"); // 写入属性文件中
System.out.println("属性文件写入成功!");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行结果: