1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.util.Properties; 4 5 public class Property { 6 public static void main(String[] args) { 7 Properties p = new Properties(); 8 int counter = 1; 9 try { 10 p.load(new FileInputStream("counter.txt")); 11 } catch (Exception e) { 12 p.setProperty("counter", String.valueOf(counter)); 13 } 14 counter = Integer.parseInt(p.getProperty("counter")); 15 System.out.println("第" + counter + "次运行"); 16 17 counter++; 18 p.setProperty("counter", String.valueOf(counter)); 19 try { 20 p.store(new FileOutputStream("counter.txt"), 21 "This software is runned counter:");// 第二个参数是对这个文件的注释,不支持中文的 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 26 } 27 }