Properties集合应用


Propertise

      特点:

                   1.该集合中键和值都是字符串类型

                   2.集合中的数据可以保存到流中,也可以从流中获取

                    

                    通常该集合用于操作以键值对形式存在的配置文件

         方法:

           list(System.out)

将集合中的数据通过标准输出流打印到控制台上

                  

                   store(OutpustStreamos,String content);//将数据存储到文件中

                  

                            Propertiesprop = new Properties();

                            prop.setProperty("kk","ssd");

                            FileOutputStreamfos = new FileOutputStream("a.txt");//字符流也可以

                            prop.store(fos,"info");

                            fos.close();

                           

                   load(InputStreamin)//将文件中的数据输入到集合中

                            FileInputStreamins = new FileInputStream("a.txt");

                            prop.load(ins);

                            prop.list(System.out);

                            ins.close();

           load方法原理

                            publicstatic void methodDemo(){

                                     propertiesprop = new properties():

                                     BufferedReaderbr = new BufferedReader(new FileReader("a.txt"));

                                     Stringline = null;

                                     while((line=br.readLine())!=null){

                                               if(line.startsWith("#"))

                                               continue;

                                               Stringstr[] = line.spilt("=");

                                               prop.setProperty(str[0],str[1]);

                                     }

                                     br.close();

                            }

           //修改配置文件

                            publicstatic void update(){

                                     Filef = new File("x.txt"):

                                     FileReaderfr = new FileReader(f);

                                     Propertiespro = new Properties():

                                     pro.load(fr);

                                     fr.close();

                                     pro.setProperty("a","b");

                                     FileWriterfw = new FileWriter(f);//注意此处是新创建了一个文件。

                                     pro.store(fw);

                                     fw.close():

                                     }

                   //定义功能:获取一个应用程序运行的次数,如果超过5次给出使用次数已到请注册的提示,并不要在运行程序

                   思路:

                            1.应该有计数器

                                    每次程序启动都需要计数一次,并且在原有的基础上计数

                            2.计数器是一个变量。

                            3.首先,程序启动时,应该先读取这个用于记录计数器信息的配置文件

                                    获取上一次计数,并进行使用次数的判断。

                                    其次,对该次数自增,并且增后的次数重新写入到配置文件中

                            4.map+io=properties

                            publicstatic void propertiesTest() throws IOException {

                            //1创建配置文件对象

                            Filefile = new File("count.properties");

                            //2.判断该文件对象是否存在

                            if(!file.exists()){

                                     file.createNewFile();

                            }

                            //3.读取该配置文件

                            FileReaderfr = new FileReader(file);

                            //4.创建和流关联的集合

                            Propertiesprop = new Properties();

                            prop.load(fr);

                            Stringvalue = prop.getProperty("time");

                            intcount = 0;

                            if(value!=null){

                                     count= Integer.valueOf(value);

                                     if(count>=5){

                                               thrownew RuntimeException("已经到五次,给钱使用");

                                     }

                            }

                            count++;

                            System.out.println("你还有"+(5-count)+"次使用机会,请继续使用");

                            //5.修改集合中的数据

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

                            //6.将增加后的值写入到配置文件

                            FileWriterfw = new FileWriter(file);

                            prop.store(fw,"");

                            fw.close();

                            fr.close();

                   }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值