关于properties+io实现计数器,输出流初始化位置不同导致不同结果的问题

YogW 2017-04-16 06:32:13
class  RunCount
{
public static void main(String[] args) throws IOException
{
Properties prop = new Properties();

File file = new File("count.ini");
if(!file.exists())
file.createNewFile();

//FileOutputStream fos = new FileOutputStream(file);//在此处计数器始终为1次
FileInputStream fis = new FileInputStream(file);
prop.load(fis);

//FileOutputStream fos = new FileOutputStream(file);//在此处计数器到达五次后再执行会重新变回1次

int count = 0;
String value = prop.getProperty("time");

if(value!=null)
{
count = Integer.parseInt(value);
if(count>=5)
{
System.out.println("您好,使用次数已到.");
return ;
}

}

count++;

prop.setProperty("time",count+"");

FileOutputStream fos = new FileOutputStream(file);//在此处正常

prop.store(fos,"");

fos.close();
fis.close();

}
}

代码如上,有没有大佬帮忙解释一下这是为什么啊?谢谢了
...全文
166 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
galiniur0u 2017-04-16
  • 打赏
  • 举报
回复
楼主可以查看fileoutputstream,这个实例创建时会清空文件,或者追加文件,楼主的就是将文件清空了,导致读取不到数据导致。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧