Properties 类

Properties 类

  • 概述
    Properties extends Hashtable implements Map:用于和io流结合,将数据通过字符流读取/写入集合中。

  • 构造方法

    构造方法描述
    Properties()创建一个没有默认值的空属性列表。
    重点方法描述
    void store​(OutputStream out, String comments)将此 Properties表中的此属性列表(键和元素对)以适合使用 load(InputStream)方法加载到 Properties表的格式写入输出流。
    void store​(Writer writer, String comments)将此 Properties表中的此属性列表(键和元素对)以适合使用 load(Reader)方法的格式写入输出字符流。
    void load​(InputStream inStream)从输入字节流中读取属性列表(键和元素对)。
    void load​(Reader reader)以简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
  • 将集合中的数据通过字符流(也可以是字节流)写入文件

    public class Demo {
        public static void main(String[] args) throws IOException {
            //将Properties中数据写入文件中
            Properties prop = new Properties();
            prop.put("count","0");
            prop.put("money","1");
            FileWriter fw = new FileWriter("h:\\360sd\\1.txt");
            prop.store(fw,null);
            fw.close();
        }
    }
    ---*---
    输出结果:
    h:\\360sd\\1.txt 文件中被写入了:
    #Sat May 15 18:18:00 CST 2021
    count=0
    numbler=1
    

    存储到properties集合的数据都以object接收,所以不指定泛型。使用 properties 中的 store 方法将集合中的数据通过字符输出流写入文件中

  • 将文件中的数据通过字符流(也可以是字节流)读取到集合, 并遍历

    public class Demo {
        public static void main(String[] args) throws IOException {
            //将上面存入的数据读取到集合
            Properties prop = new Properties();
            FileReader fr = new FileReader("h:\\\\360sd\\\\1.txt");
            prop.load(fr);
            fr.close();
            //遍历集合
            Set<Object> objects = prop.keySet();
            for (Object object : objects) {
                Object o = prop.get(object);
                System.out.println(object+","+o);
            }
        }
    }
    ---*---
    输出结果:
    count,0
    numbler,1
    

    使用 properties 中的 load 方法将文件中的数据通过 字符输入流 加载到集合。

  • 限制游戏次数
    需求:程序实现猜数字小游戏只能试玩3次,3次之后提示请充值

    public class Demo {
        public static void main(String[] args) throws IOException,ClassNotFoundException  {
            //创建properties集合
            Properties prop = new Properties();
            //字符输入流
            FileReader fr = new FileReader("e:\\360sd\\1.txt");
            //将文件中是数据读取到集合
            prop.load(fr);
            fr.close();
    		//拿到集合中count对应的值并转为int类型 
            int count = Integer.parseInt((String) prop.get("count"));
    		//判断count是否在试玩次数中。
            if (count<=2){
                game();
                //count加1 并写入文件中
                count++;
                prop.put("count",String.valueOf(count));
                FileWriter fw = new FileWriter("e:\\360sd\\1.txt");
                prop.store(fw,null);
                fw.close();
            } else {
                System.out.println("游戏次数以超过3次,请购买 http://www.baidu.com");
            }
    
        }
    	//猜数字游戏
        public static void game(){
            Random random = new Random();
            int i1 = random.nextInt(1)+1;
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个整数:");
                int i = sc.nextInt();
                if (i > i1) {
                    System.out.println("你输入是数字"+i+"大了");
                } else if(i < i1){
                    System.out.println("你输入的数字"+i+"小了");
                }else {
                    System.out.println("恭喜猜中了");
                    break;
                }
            }
        }
    }
    ---*---
    输出结果:
    当游戏玩了3次之后,将提示充值	
    //在程序运行之前 e:\\360sd\\1.txt 文件中已经保存了count=0 这个数据
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值