问题:如注释中所示,流对象放在上面的位置,不管运行几次,配置文件中的time对应的值一直是1。why?
/*
* 编写一个程序,运行5次后就需要注册
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
File f = new File("e:\\count.ini");
if(!f.exists())
f.createNewFile();
FileInputStream fis = new FileInputStream(f);
// FileOutputStream fos = new FileOutputStream(f); //放到这为啥不行?
pro.load(fis);
int count = 0;
String value = pro.getProperty("time");
if(value!=null){
count = Integer.parseInt(value);
if(count >=5)
System.out.println("请注册!");
}
count++;
pro.setProperty("time", count+"");
FileOutputStream fos = new FileOutputStream(f); //放在这里却可以
pro.store(fos,"");
fis.close();
fos.close();
}
}
|
|