Properties实例

来源:互联网 发布:网络线路自动切换 编辑:程序博客网 时间:2024/06/06 01:24
23      /********************设计一个程序,当运行一次程序,配置文件自增1*****************
***/
24      //      思路:因为程序要一一对应关系取值,所以用到Map集合,又因要从文件中读取,所以应用Properties集合
25      //   程序中要读写文件,所以要用到FileInputStream和FileWriter
26      public static void propertyAdd()throws IOException
27      {
28              Properties prop=new Properties();
29              File f=new File("info.ini");
30              if(!(f.exists()))
31                      f.createNewFile();
32              FileInputStream fis=new FileInputStream(f);
33              prop.load(fis);
34              int count=0;
35              String value=prop.getProperty("name");
36              if(value!=null)
37                      count=Integer.parseInt(value);
38              FileWriter fw=new FileWriter(f);
39              prop.setProperty("name",++count+"");      //修改键值
40              //count++;
41              prop.store(fw,"");
42              if(count>=5)
43              {
44                      System.out.println("运行次数太多了");
45                      return;
46              }
47      }
48}
0 0