(90)Properties在IO中应用:配置文件(store、load)

Properties:①是HashTable的子类,也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串
②是集合中和IO技术相结合的集合容器。
③该对象的特点,也可以用于键值对形式的配置文件。
加载数据时需要固定的格式。通常是键=值
public Set stringPropertyNames():返回所有键值

    Properties prop=new Properties();

        prop.setProperty("zhangsan", "30");
        prop.setProperty("lisi", "39");

        //String value=prop.getProperty("zhangsan");
        //System.out.println(value);

        Set<String> s=prop.stringPropertyNames();
        Iterator<String> it=s.iterator();
        while(it.hasNext()) {
            String str=it.next();
            System.out.print("键:"+str);
            System.out.print("值:"+prop.getProperty(str));
            System.out.println();
        }

一、想要将infor.txt中的键值数据存入到集合中进行操作
思路:
1,用一个流和infor.txt文件关联
2,读取一行数据,将该行数据用“=”进行切割
3,等号左面作为键,右面作为值,存入到Propertied集合中即可

import java.util.*;
import java.io.*;
public class propertiesDemo {
public static void GetAndSet() {

    Properties prop=new Properties();

    prop.setProperty("zhangsan", "30");
    prop.setProperty("lisi", "39");

    //String value=prop.getProperty("zhangsan");
    //System.out.println(value);

    Set<String> s=prop.stringPropertyNames();
    Iterator<String> it=s.iterator();
    while(it.hasNext()) {
        String str=it.next();
        System.out.print("键:"+str);
        System.out.print("值:"+prop.getProperty(str));
        System.out.println();
    }
}

    public static void main(String[] args) {
        Properties prop=new Properties();
        FileReader fr=null;
        BufferedReader bufr=null;
        try {
         fr=new FileReader("E:\\infor.txt");
         bufr=new BufferedReader(fr);
        String line=null;
        while((line=bufr.readLine())!=null) {

            String[] str=line.split("=");
            prop.setProperty(str[0], str[1]);
        }
        Set<String> s=prop.stringPropertyNames();
        Iterator<String>it=s.iterator();
        while(it.hasNext()) {
            String str=it.next();
            System.out.println("键:"+str+"    值:"+prop.getProperty(str));
        }

        }catch(IOException e) {
            throw new RuntimeException("读取失败");
        }
        finally {
            try {
                if(bufr!=null)
                    bufr.close();

            }catch(IOException e){
                throw new RuntimeException("关闭失败");

            }

        }


    }

}

其实这是public void load(InputStream inStream) throws IOException的原理,此方法就能完成如上的寻找键值的过程。其功能是从输入流中读取属性列表。用该方法实现将键值对存入集合中。
public void list(PrintStream out):将属性列表输出到指定的输出流,这个指定的输出流可以是控制台:System.out

FileInputStream fis=null;

        try {
         fis=new FileInputStream("E:\\infor.txt");
         Properties prop=new Properties();

         //将流中数据加载进集合
         prop.load(fis);
          prop.list(System.out);
            }

        }catch(IOException e) {
            throw new RuntimeException("读取失败");
        }关闭资源动作省略   

public void store(OutputStream out, String comments) throws IOException : 以适合使用load(InputStream)方法加载到Properties表中,将此Properties表中的属性列表(键值对)写入输出流。
在内存(Properties)中修改数据,存入到文件中

  FileInputStream fis=null;
        try {
         fis=new FileInputStream("E:\\infor.txt");

         Properties prop=new Properties();

         //将流中数据加载进集合
         prop.load(fis);
        // prop.list(System.out);
         prop.setProperty("language", "english");

        FileOutputStream  fos=new FileOutputStream("E:\\infor.txt");
         prop.store(fos, "haha");

         prop.list(System.out);
        }catch(IOException e) {
            throw new RuntimeException("读取失败");
        }关闭资源动作省略

需求:用于记录应用程序运行次数,如果使用次数一道,那么给出注册提示。
很容易想到的是:计数器
可是若该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。
可是随着程序的退出,该计数器也就在内存中消失。下一次在启动改程序,又重新开始从0计数,这不是我们想要的。
我们想要的是程序即使结束,该计数器的值也存在,下次程序启动会先加载该计数器的值,并加1后重新存储起来。
所以要建立一个配置文件,用于记录该软件的使用次数,该配置文件使用键值对的形式,这样便于阅读数据和操作数据。
键值对数据是map集合,数据是以文件形式存储,使用io技术,map+io–>properties.配置文件可以实现应用程序的共享

//开始时并没有这个配置文件,所以要先建立文件
        File file=new File("E:\\count.ini");
        try {
            if(!file.exists())
                file.createNewFile();
        } catch (IOException e1) {

            throw new RuntimeException("配置文件创建失败");
        }


        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
               Properties prop=new Properties();
               fis=new FileInputStream(file);
               prop.load(fis);//将文件加载到集合

               int count=0;
              String value=prop.getProperty("time");//开始时没有time,则count++,即可
              if(value!=null)
               {
                    count=Integer.parseInt(value);
                    if(count>=5) 
                     {
                       System.out.println("免费使用次数已到!!");
                       return ;
                      }
                }  


                count++;
                prop.setProperty("time",count+"");//可以设置之前有的或者没有的键值对

                 fos=new FileOutputStream(file);
                 prop.store(fos,"注释");


        }catch(IOException e) {
            throw new RuntimeException("文件写入失败");
        }
        finally {

            try {
                if(fos!=null)
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(fis!=null)
                  fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }       
posted @ 2017-07-24 09:04  测试开发分享站  阅读(97)  评论(0编辑  收藏  举报