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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 席杰 中级黑马   /  2014-9-15 14:03  /  650 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

下面是我编的程序,用于记录应用程序的运行次数,如果次数已到,那么就给出注册提示。
import java.io.*;
import java.util.*;
public class day1
{
        public static void main(String[] args) throws IOException{
                Properties properties=new Properties();
                FileReader fr=new FileReader("G:\\aim.txt");
                FileWriter fw=new FileWriter("G:\\aim.txt");1定义输出流

                properties.load(fr);
                String st=properties.getProperty("times");
                System.out.println(st);2打印读到的字符串
                if(st==null){
                        properties.setProperty("times","1");
                        //fw=new FileWriter("G:\\aim.txt");3
                        properties.store(fw,"");

                }else{
                        int count=Integer.parseInt(st);
                        if(count>=5){
                                System.out.println("次数已到,请掏钱。");
                                return;
                        }
                        count++;
                        properties.setProperty("times",count+"");
                        //fw=new FileWriter("G:\\aim.txt");3
                        properties.store(fw,"");
                }
                fr.close();
                fw.close();               
        }       
}
上述代码,为什么,我在G:\\aim.txt文件中以写了Properties键值对“times=1”;而每当我在1处定义了输出流,在2处打印出来就是null。将1处定义的输出流定义在下面用的3地方,就没问题。求解!!!

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马