IO —— 属性文件的使用

来源:互联网 发布:易观智库 数据来源 编辑:程序博客网 时间:2024/05/29 12:28

示例1:Properties 最基本的用法,当做 Map 来使用

/** * Created by liwei on 16/7/21. * * Properties 属性集合类,这个类可以和 IO 流相结合使用。 * Properties 可以保存在流中,或者从流中加载。属性列表中每一个键和其对应的值都是一个字符串。 它是 Hashtable 类的一个子类,说明是一个 Map 集合。 */public class PropertiesDemo {    public static void main(String[] args) {        // 作为Map集合的使用        // 下面这种用法是错误的,一定要看API,如果没有<>,就说明该类不是一个泛型类,在使用的时候就不能加泛型        // Properties<String, String> prop = new Properties<String, String>();        Properties prop = new Properties();        prop.put("name1","liwei");        prop.put("name2","zhouguang");        prop.put("name3","huzhenyu");        // 因为是 Hashtable 的子类,所以可以使用它的方法        // 遍历集合        Set<Object> keys= prop.keySet();        for(Object key:keys){            String value = (String)prop.get(key);            System.out.println(key + " -- " + value);        }    }}

示例2:Properties 的特殊之处:键和值都是字符串

/** * Created by liwei on 16/7/21. *//* * 特殊功能: * 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("key1","威明歌");        prop.setProperty("key2","彭丽媛");        prop.setProperty("key3","董文华");        prop.setProperty("key4","宋祖英");        Set<String> keys = prop.stringPropertyNames();        for(String key:keys){            String value = prop.getProperty(key);            System.out.println(key + " --- " + value);        }    }}/* * class Hashtalbe<K,V> { public V put(K key,V value) { ... } } * * class Properties extends Hashtable { public V setProperty(String key,String * value) { return put(key,value); } } */

示例3:键值对在磁盘上的存储和读取

/** * Created by liwei on 16/7/21. *//* * 这里的集合必须是Properties集合: * public void load(Reader reader):把文件中的数据读取到集合中 * public void store(Writer writer,String comments):把集合中的数据存储到文件 * * 单机版游戏: *      进度保存和加载。 *      三国群英传,三国志,仙剑奇侠传... * *      吕布=1 *      方天画戟=1 */public class PropertiesDemo3 {    public static void main(String[] args) throws IOException{        // load();        store();    }    public static void store() throws IOException{        Properties prop = new Properties();        prop.setProperty("name","zhouguang");        prop.setProperty("age","29");        prop.setProperty("xuewei","boshi");        //public void store(Writer writer,String comments):把集合中的数据存储到文件        Writer writer = new FileWriter("prop2.txt");        prop.store(writer,"我的光哥");        writer.close();    }    public static void load() throws IOException {        Properties prop = new Properties();        // public void load(Reader reader):把文件中的数据读取到集合中        // 注意:这个文件的数据必须是键值对形式        Reader reader = new FileReader("prop.txt");        prop.load(reader);        System.out.println(prop);        Set<String> keys = prop.stringPropertyNames();        for(String key:keys){            String value = prop.getProperty(key);            System.out.println(key + " --- " + value);        }        // 只要是流,打开以后都要关闭        reader.close();    }}

例4:同上一个例子

/** * Created by liwei on 16/7/21. *//* * 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。 * 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100” * * 分析: *      A:把文件中的数据加载到集合中 *      B:遍历集合,获取得到每一个键 *      C:判断键是否有为"lisi"的,如果有就修改其值为"100" *      D:把集合中的数据重新存储到文件中 */public class PropertiesDemo4 {    public static void main(String[] args) throws IOException {        // 把文件中的数据加载到集合中        Properties prop = new Properties();        Reader reader = new FileReader("user.txt");        prop.load(reader);        // 遍历集合,获取得到每一个键        Set<String> keys = prop.stringPropertyNames();        // 判断键是否有为"lisi"的,如果有就修改其值为"100"        for(String key:keys){            if("lisi".equals(key)){                prop.setProperty(key,"8888");                // TODO: 16/7/21                // 只要找到就退出了                break;            }        }        Writer writer = new FileWriter("user.txt");        prop.store(writer,"Users List");        reader.close();        writer.close();    }}

例5:一个小游戏,猜数字,只能限制玩 5 次

/** * Created by liwei on 16/7/21. *//* * 我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。 */public class GuessNumberGame {    public static void main(String[] args) throws IOException {        // 读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"        // 创建一个文件        // File file = new File("count.txt");        // if (!file.exists()) {        // file.createNewFile();        // }// 把数据加载到集合中        Properties properties = new Properties();        Reader reader = new FileReader("count.txt");        properties.load(reader);        reader.close();        String value = properties.getProperty("count");        Integer number = Integer.valueOf(value);        if(number>5){            System.out.println("游戏结束,请付费了哟。");            System.exit(0);        }else {            number ++ ;            Writer writer = new FileWriter("count.txt");            properties.setProperty("count",String.valueOf(number));            properties.store(writer,"game");            writer.close();            GuessNumber.start();        }    }}

游戏的代码:

/** * Created by liwei on 16/7/21. * 猜数字的小游戏 */public class GuessNumber {    public GuessNumber(){    }    public static void start(){        // 产生一个随机数        // // TODO: 16/7/21 思考为什么这里使用 Integer 不行呢        int number = (int) (Math.random()*100) +1;        // 定义一个统计变量        Integer count = 0;        while (true){            Scanner sc = new Scanner(System.in);            System.out.println("请输入 1 到 100 的一个整数");            Integer guessNumber = sc.nextInt();            count++;            // 判断            if(guessNumber > number){                System.out.println("你猜的数 "  + guessNumber + "大了");            }else if(guessNumber < number){                System.out.println("你猜的数 " + guessNumber + "小了");            }else {                System.out.println("恭喜你,你猜对了。");                System.out.println("恭喜你," + count + "次就猜中了");                break; // 跳出循环            }        }    }}
0 0
原创粉丝点击