Java基础之其他流-Properties

Properties的用法
1)Properties作为Map集合的使用
package com.huawei.honor4;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
public static void main(String[] args) {
//创建集合对象
// Properties<String,String> prop = new Properties<String,String>();//error
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);
    }
}

}
2)Properties作为Map集合的特有使用方法
package com.huawei.honor4;

import java.util.Properties;
import java.util.Set;

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

prop.setProperty("itheima001","林青霞");

prop.setProperty("itheima002","张曼玉");
prop.setProperty("itheima003","王祖贤");

// System.out.println(prop.getProperty(“itheima001”));
// System.out.println(prop.getProperty(“itheima0011”));
Set names = prop.stringPropertyNames();

    for(String key:names){

// System.out.println(key);
String value = prop.getProperty(key);
System.out.println(key+","+value);
}

// System.out.println(prop);

}

}
3)Properties和IO流的结合
package com.huawei.honor4;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

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

    //把文件中的数据加载到集合
    myLoad();
}

private static void myLoad() throws IOException {
    Properties prop = new Properties();

    FileReader fr = new FileReader("myOtherStream\\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","王祖贤");

    FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
    prop.store(fw,null);
    fw.close();
}

}
4)游戏练习-猜数字
//GuessNumber类
package com.huawei.honor4;
import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
private GuessNumber(){
}

public static void start(){
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(guessNumber>number){
           System.out.println("你猜的数字:"+guessNumber+"大了");
       }else if(guessNumber<number){
           System.out.println("你猜的数字:"+guessNumber+"小了");
       }else{
           System.out.println("恭喜你猜对了");
           break;
       }
   }

}
}
//测试类
package com.huawei.honor4;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();

    FileReader fr = new FileReader("myOtherStream\\game.txt");
    prop.load(fr);
    fr.close();

    //通过Properties集合获取到玩游戏的次数
    String count = prop.getProperty("count");
    int number = Integer.parseInt(count);

    if(number>=3){
        System.out.println("游戏已结束,想玩请充值(www.itcast.cn)");
    }else{
        GuessNumber.start();
        number++;
        prop.setProperty("count",String.valueOf(number));
        FileWriter fw = new FileWriter("myOtherStream\\game.txt");
        prop.store(fw,null);
        fw.close();
    }
}

}
//在项目文件夹下有game.txt文件
count=0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值