Properties集合
2016-03-10 14:49
423 查看
Properties:属性集合类。是一个可以和IO流相结合使用的集合类。 Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 是Hashtable的子类,说明是一个Map集合。
package cn.itcast_08; import java.util.Properties; import java.util.Set; 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("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); } } }
特殊功能: public Object setProperty(String key,String value):添加元素 public String getProperty(String key):获取元素 public Set<String> stringPropertyNames():获取所有的键的集合
package cn.itcast_08; import java.util.Properties; import java.util.Set; 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); } } } /* * 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); } } */
这里的集合必须是Properties集合: public void load(Reader reader):把文件中的数据读取到集合中 public void store(Writer writer,String comments):把集合中的数据存储到文件 单机版游戏: 进度保存和加载。 三国群英传,三国志,仙剑奇侠传... 吕布=1 方天画戟=1
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; 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 w = new FileWriter("name.txt"); prop.store(w, "helloworld"); w.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:把集合中的数据重新存储到文件中
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; import java.util.Set; 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次提示:游戏试玩已结束,请付费。
package cn.itcast_08; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.Properties; 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(); } } }
相关文章推荐
- linux学习笔记:用户管理命令
- Debian 7(Linux) 安装SSH使用SecureCRT连接配置
- Linux中限制用户访问权限的3种方法
- centos6.5中gitlab的搭建
- linux创建文件树,孩子兄弟树(或广义表),创建文件树及其訪问
- linux下Yum搭建lamp网站架构
- arm-linux-gcc交叉工具链的安装和使用
- Docker镜像
- window.open 设置高和宽无效
- tomcat启动过程报the JDBC Driver has been forcibly unregistered问题的修复过程
- Linux 容器技术的三大致命隐忧
- linux 下使用gdb调试总结
- sed详解
- Mybatis中传参报There is no getter for property name 'xxx'
- 利用网站接口做工具
- Docker Hub仓库注册,使用
- AOP 入门
- jmeter在linux上启动报错:连接拒绝
- centos精简系统 源码安装客户端git
- centos7 的防火墙命令调整了