day19Java-其它IO-Properties-属性集合类(可以结合IO)

博客名称
Java-(中级)

Properties-属性集合类(可以结合IO)

java.util.Dictionary<K,V> 继承者 java.util.Hashtable<Object,Object> 继承者 java.util.Properties

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("001", "挖掘机");
        prop.put("002", "男枪");
        prop.put("003", "盲僧");

        //遍历Map集合方式1
        for (Map.Entry<Object, Object> me : prop.entrySet()) {
            System.out.println(me.getKey() + "--" + me.getValue());
        }
        System.out.println("---------------------");
        //遍历方式2
        for (Object o:prop.keySet()) {
            System.out.println(o+"--"+prop.get(o));

        }
    }
}

结果:

001--挖掘机
002--男枪
003--盲僧
---------------------
001--挖掘机
002--男枪
003--盲僧
Properties-特殊功能

public Object setProperty(String key,String value):添加元素
public String getProperty(String key):获取元素
public Set stringPropertyNames():获取所有的键的集合

代码演示

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

        //public Object setProperty(String key,String value):添加元素
        //底层使用Map集合的put方法实现的,但是这里传入的参数只能是字符串
        //添加元素
        prop.setProperty("卢锡安", "44");
        prop.setProperty("艾希", "30");
        prop.setProperty("希维尔", "43");

        //遍历集合
        //public Set<String> stringPropertyNames():获取所有的键的集合
        Set<String> set = prop.stringPropertyNames();
        for (String s : set) {
            //public String getProperty(String key):获取元素
            System.out.println(s + "--" + prop.get(s));
        }
    }
}
/*
 * 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); } }
 */

结果:

卢锡安--44
希维尔--43
艾希--30
Properties-load()和store()功能

如果只能使用一个集合来读写文件那就是Properties集合

这里的集合必须是Properties集合:
public void load(Reader reader):把文件中的数据读取到集合中
public void store(Writer writer,String comments):把集合中的数据存储到文件

单机版游戏:
  		进度保存和加载。
  		三国群英传,三国志,仙剑奇侠传...
  
  		吕布=1
  		方天画戟=1

代码演示1
public void load(Reader reader):把文件中的数据读取到集合中

public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
        //将集合数据存储到文件中
        myStore();
    }

    public static void myStore() throws IOException {
        //创建集合对象
        Properties prop = new Properties();

        //添加元素
        prop.setProperty("001","亚索");
        prop.setProperty("002","伊芙琳");
        prop.setProperty("003","菲兹");

        //创建字符流对象
        FileWriter fw = new FileWriter("prop.txt");
        //public void store(Writer writer,String comments):把集合中的数据存储到文件
        prop.store(fw,"hello");
        //释放资源
        fw.close();
    }
}

结果:prop.txt文件内容

#hello
#Wed Apr 01 14:12:09 CST 2020
001=亚索
002=伊芙琳
003=菲兹

代码演示2
public void store(Writer writer,String comments):把集合中的数据存储到文件

public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
        //将集合数据存储到文件中
       // myStore();

        //读取文件中的键值对数据
        myLoad();
    }
    
    public static void myStore() throws IOException {
        //创建集合对象
        Properties prop = new Properties();

        //添加元素
        prop.setProperty("001","亚索");
        prop.setProperty("002","伊芙琳");
        prop.setProperty("003","菲兹");

        //创建字输出符流对象
        FileWriter fw = new FileWriter("prop.txt");
        //public void store(Writer writer,String comments):把集合中的数据存储到文件
        prop.store(fw,"hello");
        //释放资源
        fw.close();

    }
    public static void myLoad() throws IOException {
        //创建集合对象
        Properties prop = new Properties();

        //创建字输入符流对象
        FileReader fr = new FileReader("prop.txt");
        // public void load(Reader reader):把文件中的数据读取到集合中
        //注意:这个文件的数据必须是键值对形式
        prop.load(fr);
        fr.close();
        System.out.println("prop:"+prop);
    }
}

结果:

prop:{001=亚索, 002=伊芙琳, 003=菲兹}
我有一个文本文件(prop.txt),我知道数据是键值对形式的,但是不知道内容是什么。

请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”
prop.txt文件

lisi=40
zhangsan=30
wangwu=50
分析:
        1.创建Properties对象
        2.读取user.txt文件;
        3.在获取到每一个键,在判断改建是否包含lisi
                是:就把值替换为100
              不是:不搭理
        4.创建字符缓冲输出流对象,重新写入文件。

代码演示

public class ProperetiesTest {
    public static void main(String[] args) throws IOException {
        //创建集合对象
        Properties prop = new Properties();
        //创建字符输入流对象
        FileReader fr= new FileReader("prop.txt");
        //读取数据
        prop.load(fr);
        System.out.println(prop);
        //获取键集合
        Set<String> set = prop.stringPropertyNames();
        for (String key:set){
            if(key.equals("lisi")){
                prop.setProperty(key,"100");
                break;
            }
        }

        //创建字符输出流缓冲对象
        FileWriter fw = new FileWriter("prop.txt");
        prop.store(fw,null);
        //关闭资源
        fw.close();

    }
}

结果:prop.txt文件中内容

#Wed Apr 01 14:37:21 CST 2020
lisi=100
zhangsan=30
wangwu=50
我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
分析:
	1.创建集合对象,创建字符缓冲输入流对象
	2.调用集合方法读取文件
	3.根据count键,获取值,在判断改值是否大于5
								大于5:请付费
								小于5:键对应的值做++,调动方法从新设置count的值在写到文件中

代码演示

public class ProperetiesTest2 {
    public static void main(String[] args) throws IOException {
        //创建集合对象
        Properties prop  = new Properties();
        //创建字符缓冲输入流
        FileReader fr= new FileReader("prop.txt");
        //读取数据
        prop.load(fr);

        //这个程序是我自己写的,我当然之后文件中存储的是什么
        String strcount = prop.getProperty("count");
        int count = Integer.parseInt(strcount);
        //判断该值是否大于5
        if(count>5){
            System.out.println("你已经免费体验了5次了,需要付费了!");
        }else{
            count++;
            //玩游戏了
            GuessNumber.start();
            FileWriter fw = new FileWriter("prop.txt");
            //从新设置参数值
            prop.setProperty("count",String.valueOf(count));
            //写数据
            prop.store(fw,null);
        }

    }
}

游戏类

public class GuessNumber {
	private GuessNumber() {
	}

	public static void start() {
		// 产生一个随机数
		int number = (int) (Math.random() * 100) + 1;

		// 定义一个统计变量
		int count = 0;

		while (true) {
			// 键盘录入一个数据
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入数据(1-100):");
			int guessNumber = sc.nextInt();

			count++;

			// 判断
			if (guessNumber > number) {
				System.out.println("你猜的数据" + guessNumber + "大了");
			} else if (guessNumber < number) {
				System.out.println("你猜的数据" + guessNumber + "小了");
			} else {
				System.out.println("恭喜你," + count + "次就猜中了");
				break;
			}
		}
	}
}

结果:

你已经免费体验了5次了,需要付费了!

再连续执行5次代码后prop.txt文件中count已经等于6了,所以要付费了。

#Wed Apr 01 15:24:28 CST 2020
count=6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值