首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

getPropery的小疑点请求。

2012-05-27 
getPropery的小问题请求。。程序一:Java code/*需求:用于记录应用程序运行次数,如果使用次数已到5次,就提示

getPropery的小问题请求。。

程序一:

Java code
/*需求:    用于记录应用程序运行次数,如果使用次数已到5次,就提示注册*/import java.io.*;import java.util.*;class RunCount{    public static void main(String[] args)throws IOException     {        Properties prop = new Properties();        //创建一个配置文件对象        File file = new File("count.ini");        FileOutputStream fos = new FileOutputStream(file);        if(!file.exists())            file.createNewFile();        //建读取流对象关联配置文件        FileInputStream fis = new FileInputStream(file);        //建立写出流对象关联配置文件        prop.load(fis);                                        //定义一个计数器,执行一次,计数器加 1,并写入到配置文件中        int count = 0;        String value = prop.getProperty("times");        System.out.println(value);        if(value!=null)        {            count = Integer.parseInt(value);            if(count==5)            {                System.out.println("请注册");            }        }                count++;        prop.setProperty("times",count+"");                prop.store(fos,"");        fis.close();        fos.close();    }}


程序二:
Java code
/*需求:    用于记录应用程序运行次数,如果使用次数已到5次,就提示注册*/import java.io.*;import java.util.*;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();        //建读取流对象关联配置文件        FileInputStream fis = new FileInputStream(file);        //建立写出流对象关联配置文件        prop.load(fis);                                        //定义一个计数器,执行一次,计数器加 1,并写入到配置文件中        int count = 0;        String value = prop.getProperty("times");        System.out.println(value);        if(value!=null)        {            count = Integer.parseInt(value);            if(count==5)            {                System.out.println("请注册");            }        }                count++;        prop.setProperty("times",count+"");        FileOutputStream fos = new FileOutputStream(file);        prop.store(fos,"");        fis.close();        fos.close();    }}


程序一和程序二没什么区别,就是将 FileOutputStream fos = new FileOutputStream(file);的顺序改变了一下,为什么程序一错误,而程序二正确呢?


[解决办法]
File file = new File("count.ini");
FileOutputStream fos = new FileOutputStream(file);

会直接生产一个空的文件。(之前文件中有内容的话,也会清空。)

这样你就读不到文件中的内容了。

热点排行