java Properties集合与IO流

Properties介绍

  • 是一个Map体系的集合类
  • Properties可以保存到流中或从流中加载
  • 属性列表中的每个键及其对应的值都是一个字符串

特有方法

  • 方法名说明
    Object setProperty(String key, String value)设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
    String getProperty(String key)使用此属性列表中指定的键搜索属性
    Set stringPropertyNames()从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串

示例代码

public class PropertiesDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop = new Properties();

        //Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
        prop.setProperty("itheima001", "林青霞");
        /*
            Object setProperty(String key, String value) {
                return put(key, value);
            }

            Object put(Object key, Object value) {
                return map.put(key, value);
            }
         */
        prop.setProperty("itheima002", "张曼玉");
        prop.setProperty("itheima003", "王祖贤");

        //String getProperty(String key):使用此属性列表中指定的键搜索属性
//        System.out.println(prop.getProperty("itheima001"));
//        System.out.println(prop.getProperty("itheima0011"));

//        System.out.println(prop);

        //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set<String> names = prop.stringPropertyNames();
        for (String key : names) {
//            System.out.println(key);
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }
}

Properties和IO流相结合的方法

  • 和IO流结合的方法
方法名说明
void load(InputStream inStream)从输入字节流读取属性列表(键和元素对)
void load(Reader reader)从输入字符流读取属性列表(键和元素对)
void store(OutputStream out, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流
  • 示例代码
public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
        //把集合中的数据保存到文件
//        myStore();

        //把文件中的数据加载到集合
        myLoad();

    }

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

        //void load(Reader reader):
        FileReader fr = new FileReader("myOtherStream\\fw.txt");
        prop.load(fr);
        fr.close();

        System.out.println(prop);
    }

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

        prop.setProperty("itheima001","林青霞");
        prop.setProperty("itheima002","张曼玉");
        prop.setProperty("itheima003","王祖贤");

        //void store(Writer writer, String comments):
        FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
        prop.store(fw,null);
        fw.close();
    }
}

游戏次数案例【应用】

  • 案例需求

    • 实现猜数字小游戏只能试玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
  • 分析步骤

    1. 写一个游戏类,里面有一个猜数字的小游戏

    2. 写一个测试类,测试类中有main()方法,main()方法中写如下代码:

      ​ 从文件中读取数据到Properties集合,用load()方法实现
      - 文件已经存在:game.txt
      - 里面有一个数据值:count=0

      ​ 通过Properties集合获取到玩游戏的次数

      ​ 判断次数是否到到3次了
      -如果到了,给出提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
      -如果不到3次:次数+1,重新写回文件,用Properties的store()方法实现玩游戏

  • 代码实现

public class PropertiesTest {
   public static void main(String[] args) throws IOException {
       //从文件中读取数据到Properties集合,用load()方法实现
       Properties prop = new Properties();

       FileReader fr = new FileReader("myOtherStream\\game.txt");
       prop.load(fr);
       fr.close();

       //通过Properties集合获取到玩游戏的次数
       String count = prop.getProperty("count");
       int number = Integer.parseInt(count);

       //判断次数是否到到3次了
       if(number >= 3) {
           //如果到了,给出提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
           System.out.println("游戏试玩已结束,想玩请充值(www.itcast.cn)");
       } else {
           //玩游戏
           GuessNumber.start();

           //次数+1,重新写回文件,用Properties的store()方法实现
           number++;
           prop.setProperty("count",String.valueOf(number));
           FileWriter fw = new FileWriter("myOtherStream\\game.txt");
           prop.store(fw,null);
           fw.close();
       }
   }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值