Map集合——(4) Properties 元素存取修改、List、读取文件Properties方法

目录

一、含义

1、Propertis:是Map集合中Hastable的一个子类。

2、API继承体系

3、API介绍:

4、线程安全:

二、特点:

三、常见功能代码演示

1、Properties集合 存、取、修改元素

2、Properties与Io流相结合

3、将Properties集合持久化存储到硬盘中

4、  读取文件中的键值对信息

5、自定义load( )读取 实现方法

6、对已有的配置文件中的信息进行修改。


一、含义

1、Propertis:是Map集合中Hastable的一个子类。

2、API继承体系

3、API介绍:

4、线程安全:

 

二、特点:

      1、该集合中存储的键值对都是以字符串形式存在

      2、集合中的数据都可以保存到流中,或者可以从流中取出。

      3、该集合通常操作以键值对形式存在的应用配置文件的读取。

      4、是数据持久化的一种操作。

三、常见功能代码演示

1、Properties集合 存、取、修改元素

 private static void propertiesDemo() {

        //创建一个Properties集合
        Properties prop =new Properties();
        //存入数据
        prop.setProperty("zhansan","20");
        prop.setProperty("lisi","25");
        prop.setProperty("wangwu","15");
        prop.setProperty("zhaoliu","21");


        //修改元素。
        prop.setProperty("wangwu","26");
        //取出所有元素
        Set<String> names = prop.stringPropertyNames();


        for (String key:names){
            String value=prop.getProperty(key);
            System.out.println(key+":"+value);
        }

    }

  

2、Properties与Io流相结合

API:

代码:

public static void methodDemo_2(){
        Properties prop  = new Properties();

        //存储元素。
//		prop.setProperty("zhangsan","30");
//		prop.setProperty("lisi","31");
//		prop.setProperty("wangwu","36");
//		prop.setProperty("zhaoliu","20");

        //获取平台系统信息
        prop = System.getProperties();
        //调试打印集合中所有的元素
        prop.list(System.out);
    }

结果:  

3、将Properties集合持久化存储到硬盘中

方法API:

代码:

 public static void methodDemo_3() throws IOException {
        Properties prop  = new Properties();
        //存储元素。
        prop.setProperty("zhangsan","30");
        prop.setProperty("lisi","31");
        prop.setProperty("wangwu","36");
        prop.setProperty("zhaoliu","20");

        //想要将这些集合中的字符串键值信息持久化存储到文件中。
        //需要关联输出流。
        FileOutputStream fos = new FileOutputStream("info.txt");
        /***
         * 将集合中数据存储到文件中,使用store方法。
         *
         * comm为存储说明信息
         */
        prop.store(fos, "info");
        fos.close();
    }

结果:

4、  读取文件中的键值对信息

API:

代码演示:

public static void methodDemo_4() throws IOException {	
		
		Properties prop  = new Properties();
		
		//集合中的数据来自于一个文件。 
		//注意;必须要保证该文件中的数据是键值对。
		//需要使用到读取流。 
		FileInputStream fis = new FileInputStream("info.txt");
		
		//使用load方法。 
		prop.load(fis);
		prop.list(System.out);
		
	}

结果:

5、自定义load( )读取 实现方法

 //模拟一下load方法。
    public static void myLoad() throws IOException{

        Properties prop  = new Properties();

        BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));

        String line = null;

        while((line=bufr.readLine())!=null){
            if(line.startsWith("#")) {
                continue;
            }
            String[] arr = line.split("=");

//			System.out.println(arr[0]+"::"+arr[1]);
            prop.setProperty(arr[0], arr[1]);
        }

        prop.list(System.out);

        bufr.close();

    }

结果:

6、对已有的配置文件中的信息进行修改。

思路:

读取这个文件,并将这个文件中的键值数据存储到集合中,在通过集合对数据进行修改,在通过流将修改后的数据存储到文件中。

代码实现:

public static void test() throws IOException{
        //读取这个文件。
        File file = new File("info.txt");
        //如果文件不存在就创建,减少了异常
        if(!file.exists()){
            file.createNewFile();
        }
        FileReader fr = new FileReader(file);
        //创建集合存储配置信息。
        Properties prop = new Properties();
        //将流中信息存储到集合中。
        prop.load(fr);
        prop.setProperty("wangwu", "16");
        //再次创建输出流对象
        FileWriter fw = new FileWriter(file);
        prop.store(fw,"");
//		prop.list(System.out);
        fw.close();
        fr.close();
    }

 修改前:

修改后:

发布了186 篇原创文章 · 获赞 45 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/zhanshixiang/article/details/103887227