黑马程序员技术交流社区

标题: 关于Properties类的一个问题 [打印本页]

作者: wisely    时间: 2014-7-7 20:10
标题: 关于Properties类的一个问题
本帖最后由 wisely 于 2014-7-7 20:34 编辑

上代码
  1. import java.io.*;
  2. import java.util.*;
  3. public class Monday
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 File file=new File("Property.txt");
  8.                 if(!file.exists())
  9.                         file.createNewFile();
  10.                 FileInputStream fis=new FileInputStream(file);

  11.                 //①注意下面这句代码
  12.                 FileOutputStream fos=new FileOutputStream(file);
  13.                 Properties prop=new Properties();
  14.                 prop.load(fis);
  15.                 String value=prop.getProperty("times");
  16.                 int count=0;
  17.                 if(value!=null){
  18.                         count=Integer.parseInt(value);
  19.                         if(count>=5){
  20.                                 System.out.println("Game Over!");
  21.                                 return;
  22.                         }
  23.                 }
  24.                 count++;
  25.                 prop.setProperty("times",count+"");
  26.                
  27.                 //②注意下面这句代码
  28.                 //FileOutputStream fos=new FileOutputStream(file);
  29.                 prop.store(fos,"");
  30.                 fos.close();
  31.                 fis.close();
  32.         }
  33. }
复制代码

同样一句代码,放在①的位置就起不到作用,放在②的位置就能,为啥?求教。

PS:代码来源于毕老师视频,第20天-14。





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2