Properties实现文件存储

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        Properties prot=new Properties();
        try {
            prot.load(new FileInputStream("test.txt"));
            int num=Integer.parseInt(prot.getProperty("test"))+1;
            System.out.println("这是第"+num+"次运行");
            prot.setProperty("test",new Integer(num).toString());
        } catch (Exception e) {
            prot.setProperty("test",String.valueOf(0));
            System.out.println("这是第"+String.valueOf(0)+"次运行");
        }
        try {
            //可以接收非字符串的值
            prot.store(new FileOutputStream("test.txt"),"Program is used num");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/zhangtx/p/2549732.html