JavaSE学习笔记(三十)—— Properties集合 一、Properties概述 二、Properties作为Map集合的使用 三、Properties的特殊功能 四、Properties和IO流的结合使用

  Properties是一个属性集合类。是一个可以和IO流相结合使用的集合类。

  Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

  Properties是Hashtable的子类,说明是一个Map集合。该类的继承层次为:

  JavaSE学习笔记(三十)—— Properties集合
一、Properties概述
二、Properties作为Map集合的使用
三、Properties的特殊功能
四、Properties和IO流的结合使用

  简单来说,Properties类的特点是:

  1. 该集合中的键和值都是字符串类型
  2. 集合中的数据可以保存到流中或者从流中获取数据。
  3. 通常该集合用于操作以键值对形式存在的配置文件。

二、Properties作为Map集合的使用

public class PropertiesDemo {
    public static void main(String[] args) {
        Properties prop = new Properties();

        // 添加元素
        prop.put("it002", "hello");
        prop.put("it001", "world");
        prop.put("it003", "java");

        // System.out.println("prop:" + prop);

        // 遍历集合
        Set<Object> set = prop.keySet();
        for (Object key : set) {
            Object value = prop.get(key);
            System.out.println(key + "---" + value);
        }
    }
}

三、Properties的特殊功能

  • public Object setProperty(String key,String value):添加元素
  • public String getProperty(String key):获取元素
  • public Set<String> stringPropertyNames():获取所有的键的集合
public class PropertiesDemo2 {
    public static void main(String[] args) {
        // 创建集合对象
        Properties prop = new Properties();

        // 添加元素
        prop.setProperty("张三", "30");
        prop.setProperty("李四", "40");
        prop.setProperty("王五", "50");

        // public Set<String> stringPropertyNames():获取所有的键的集合
        Set<String> set = prop.stringPropertyNames();

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

  为什么setProperty()只能存储字符串呢?它的原理是怎样的?

  Hashtalbe有一个put()方法:

class Hashtalbe<K,V> { 
    public V put(K key,V value) { ... } 
}

  Properties是Hashtable的子类,它的setProperty()方法调用的是Hashtable的方法 put(),并且强制要求为属性的键和值使用字符串

class Properties extends Hashtable { 
  public V setProperty(String key,String value) {
    return put(key,value);
  }
}

四、Properties和IO流的结合使用

  • public void load(Reader reader):把文件中的数据读取到集合中(这里的集合必须是Properties集合)
  • public void store(Writer writer,String comments):把集合中的数据存储到文件
public class PropertiesDemo3 {
    public static void main(String[] args) throws IOException {
        myLoad();
        // myStore();
    }

    private static void myStore() throws IOException {
        // 创建集合对象
        Properties prop = new Properties();

        prop.setProperty("林青霞", "27");
        prop.setProperty("武鑫", "30");
        prop.setProperty("刘晓曲", "18");

        //public void store(Writer writer,String comments):把集合中的数据存储到文件
        Writer writer = new FileWriter("name.txt");
        prop.store(writer, "helloworld");
        writer.close();
    }

    private static void myLoad() throws IOException {
        Properties prop = new Properties();

        // public void load(Reader reader):把文件中的数据读取到集合中
        Reader r = new FileReader("prop.txt");
        prop.load(r);
        r.close();

        System.out.println("prop:" + prop);
    }
}

【练习】

  我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”

/**
 * 分析:
 *         A:把文件中的数据加载到集合中
 *         B:遍历集合,获取得到每一个键
 *         C:判断键是否有为"lisi"的,如果有就修改其值为"100"
 *         D:把集合中的数据重新存储到文件中
 */
public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        // 把文件中的数据加载到集合中
        Properties prop = new Properties();
        Reader r = new FileReader("user.txt");
        prop.load(r);
        r.close();

        // 遍历集合,获取得到每一个键
        Set<String> set = prop.stringPropertyNames();
        for (String key : set) {
            // 判断键是否有为"lisi"的,如果有就修改其值为"100"
            if ("lisi".equals(key)) {
                prop.setProperty(key, "100");
                break;
            }
        }

        // 把集合中的数据重新存储到文件中
        Writer w = new FileWriter("user.txt");
        prop.store(w, null);
        w.close();
    }
}

  我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。

public class PropertiesTest2 {
    public static void main(String[] args) throws IOException {
        // 读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"
        // 创建一个文件
        // File file = new File("count.txt");
        // if (!file.exists()) {
        // file.createNewFile();
        // }

        // 把数据加载到集合中
        Properties prop = new Properties();
        Reader r = new FileReader("count.txt");
        prop.load(r);
        r.close();

        // 我自己的程序,我当然知道里面的键是谁
        String value = prop.getProperty("count");
        int number = Integer.parseInt(value);

        if (number > 5) {
            System.out.println("游戏试玩已结束,请付费。");
            System.exit(0);
        } else {
            number++;
            prop.setProperty("count", String.valueOf(number));
            Writer w = new FileWriter("count.txt");
            prop.store(w, null);
            w.close();

            GuessNumber.start();
        }
    }
}
/**
 * 这是猜数字小游戏*/
public class GuessNumber {
    private GuessNumber() {
    }

    public static void start() {
        // 产生一个随机数
        int number = (int) (Math.random() * 100) + 1;

        // 定义一个统计变量
        int count = 0;

        while (true) {
            // 键盘录入一个数据
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数据(1-100):");
            int guessNumber = sc.nextInt();

            count++;

            // 判断
            if (guessNumber > number) {
                System.out.println("你猜的数据" + guessNumber + "大了");
            } else if (guessNumber < number) {
                System.out.println("你猜的数据" + guessNumber + "小了");
            } else {
                System.out.println("恭喜你," + count + "次就猜中了");
                break;
            }
        }
    }
}