日期:2014-05-20  浏览次数:20748 次

请教一个关于流的问题
public static void fun6()throws IOException
        {
                Properties prop=new Properties();
                File file=new File("d:\\prop.ini");
                if(!file.exists())
                {
                        file.createNewFile();
                }
                FileInputStream fis=new FileInputStream(file);
                FileOutputStream fos=new FileOutputStream(file);
                prop.load(fis);

                if(prop.getProperty("time")!=null)
                {
                        String value=prop.getProperty("time");
                        int time=Integer.parseInt(value);
                        if(time>5)
                        {
                                sop("拿钱");
                                return;
                        }
                        time++;
                        prop.setProperty("time",""+time);
                        
                }
                else
                {
                        int time=0;
                        time++;
                        prop.setProperty("time",""+time);
                        //prop.store(fos,"");
                }
                
                prop.store(fos,"");
        }

FileOutputStream fos=new FileOutputStream(file);我的这个写入流定义在现在这个位置就无法完成程序的计数任务,定义在prop.store(fos,"");的前面一句就可以,这是为什么呢,我在前面定义了写入流但我没使用,和在下面定义有什么区别呢?求指教
------解决方案--------------------
很简单,因为在执行FileOutputStream fos=new FileOutputStream(file);之后,file里面的内容将清空,所以程序进入if里,time值始终为1