A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 夜写意 中级黑马   /  2013-8-28 06:36  /  1089 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码
  1. class RunCount
  2. {
  3.         Sop s = new Sop();
  4.         RunCount() throws IOException
  5.         {
  6.                 Properties prop = new Properties();
  7.                 File f = new File("D:\\JavaTest\\RunCount.ini");
  8.                 if(!f.exists())
  9.                         f.createNewFile();
  10.                 FileInputStream fis = new FileInputStream(f);
  11. 1号位:   FileOutputStream fos = new FileOutputStream(f);               
  12.                 prop.load(fis);
  13.                 int count = 0;
  14.                
  15.                 String value = prop.getProperty("Time");
  16.                 if(value!=null)                        
  17.                 {
  18.                         count = Integer.parseInt(value);
  19.                         if(count>=5)
  20.                         {
  21.                                 s.sop("Time out");
  22.                                 //return;
  23.                         }                        
  24.                 }
  25.                 count++;
  26.                 prop.setProperty("Time", count + "");
  27.                
  28. 2号位:   FileOutputStream fos = new FileOutputStream(f);               
  29.                 prop.store(fos, "");

  30.                 fis.close();
  31.                 fos.close();
  32.         }
  33.         
  34. }
复制代码
问题是:为什么放在1号位的时候,不管运行多少次,Time的值都是1.  但是放在2号位的时候,就可以正常计数?请分析下两个不同位置程序的运行过程有什么不同。

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

4 个回复

倒序浏览
输出流,在没显式调用flush()时,所输出的数据还在流中,并没有写入到指定中,只有调用了flush()强制刷新或者调用colse()时才会把流中的数据输出到指定位置。

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报
首先我不知道你问的什么,那些代码放到一号,和放到二号的啊,但是上面的运行后,count永远是1的,因为你使用的是if语句判断的:
  • Properties prop = new Properties();
  •                 File f = new File("D:\\JavaTest\\RunCount.ini");
  •                 if(!f.exists())
  •                         f.createNewFile();
  •                 FileInputStream fis = new FileInputStream(f);
  • 1号位:   FileOutputStream fos = new FileOutputStream(f);
  •                 prop.load(fis);
  •                 int count = 0;
  •                 while(true)
  •                 {
  •                 String value = prop.getProperty("Time");
  •                 if(value!=null)
  •                 {
  •                         count = Integer.parseInt(value);
  •                         if(count>=5)
  •                         {
  •                                 s.sop("Time out");
  •                                 return;
  •                         }
  •                 }
  •                 count++;
  •                 prop.setProperty("Time", count + "");
  •             }
  •             这样就count就可能不是1了。

回复 使用道具 举报
FileOutputStream fos = new FileOutputStream(f);  放在前面每次运行时会 会重建一个文件。相当于每次重新存入值。 FileOutputStream fos = new FileOutputStream(f);  放在后面每次运行时会 也会重建一个文件。但是它能保证每次读到上次存入值的文件。所以可计数
二者区别就是在重建文件时能否读到原来的文件信息。在1处没读到,而在2处已读到。

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

回复 使用道具 举报 1 0
同学你把简单的问题问得复杂了,其实不一定放在你指定的2号位新建dos对象才有效,放在prop.load(fis)后面就行,原因是dis和dos引用的参数是同一个f,只有dis的流数据被处理完,dos才能正常包装流数据。
  1. //修改后:
  2. FileInputStream fis = new FileInputStream(f);
  3. prop.load(fis);
  4. FileOutputStream fos = new FileOutputStream(f);               
  5.                
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马