zoukankan      html  css  js  c++  java
  • properties集合和io的结合

    一、Properties作为Map集合的使用
    public class PropertiesDemo01 {
    public static void main(String[] args) {
    //创建集合对象
    //Properties<String,String> prop = new Properties<String,String>();错误
    Properties prop = new Properties();

        //存储元素
        prop.put("itheima001","林青霞");
        prop.put("itheima002","张曼玉");
        prop.put("itheima003","王祖贤");
    
        //遍历集合
        Set<Object> keySet = prop.keySet();
        for (Object key : keySet) {
            Object value = prop.get(key);
            System.out.println(key + "," + value);
        }
    }
    

    }
    二、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("itheima0001"));
    //
    // 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流结合的方法:
    void load(Reader reader):
    从输入字符流读取属性列表(键和元素对)

        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("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("fw.txt");
        prop.store(fw,null);
        fw.close();
    }
    

    }
    四、具体案例实现
    需求:请写程序实现猜数字小游戏只能试玩3次,如果还想玩,提示:游戏试玩已结束,想玩请充值(www.itcast.cn)

    思路:
        1:写一个游戏类,里面有一个猜数字的小游戏
        2:写一个测试类,测试类中有main方法,main()方法中按照下面步骤完成
            A:从文件中读取数据到Properties集合,用load()方法实现
                文件已经存在:game.txt
                里面有一个数据值:count=0
            B:通过Properties集合获取到玩游戏的次数
            C:判断次数是否到到3次了
                如果到了,给出提示:游戏试玩已结束,想玩请充值(www.itcast.cn)
                如果不到3次:
                    玩游戏
                    次数+1,重新写回文件,用Properties的store()方法实现
    

    1、创建游戏类
    public class GuessNumber {
    private GuessNumber() {
    }

    public static void start() {
        //要完成猜数字的游戏,首先需要有一个要猜的数字,使用随机数生成该数字,范围1到100
        Random r = new Random();
        int number = r.nextInt(100) + 1;
    
        while (true) {
            //使用程序实现猜数字,每次均要输入猜测的数字值,需要使用键盘录入实现
            Scanner sc = new Scanner(System.in);
    
            System.out.println("请输入你要猜的数字:");
            int guessNumber = sc.nextInt();
    
            //比较输入的数字和系统产生的数据,需要使用分支语句。这里使用if..else..if..格式,根据不同情况进行猜测结果显示
            if (guessNumber > number) {
                System.out.println("你猜的数字" + guessNumber + "大了");
            } else if (guessNumber < number) {
                System.out.println("你猜的数字" + guessNumber + "小了");
            } else {
                System.out.println("恭喜你猜中了");
                break;
            }
        }
    }
    

    }

    2、创建游戏记录次数文件game.txt,内容为“count=0”

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

        FileReader fr = new FileReader("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("game.txt");
            prop.store(fw,null);
            fw.close();
        }
    }
    

    }

  • 相关阅读:
    leetcode 343. Integer Break(dp或数学推导)
    leetcode 237. Delete Node in a Linked List
    msdtc不可用
    常用反编译软件
    重建索引
    JAVA知识库
    DATAGRID显示序号
    VFLEXGRID8控件注册
    黑马2017年java就业班全套视频教程
    mybatis从入门到精通
  • 原文地址:https://www.cnblogs.com/zhaoxiangjun/p/12987858.html
Copyright © 2011-2022 走看看