Properties类的使用(JAVA)

Properties类的使用

Properties作为Map集合的使用

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

/*
 Properties作为Map集合的使用  不是泛型类
 */
public class Demo01 {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop=new Properties();

        //存储原始
        prop.put("xjy",18);
        prop.put("ysj",18);
        prop.put("xbm",18);

        //遍历集合
        Set<Object> objects = prop.keySet();
        for (Object o:objects){
            System.out.println(o+","+prop.get(o));
        }
    }
}

Properties作为集合的特有方法

/*Properties作为集合的特有方法
   方法名                                       说明
Object setProperties(String key,String value)   设置集合的键和值,都是String类型,底层调用Hashtable方法put
String getProperties(String key)                使用此属性列表中指定的键搜索属性
Set<String> stringPropertyNames()               从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
 */
public class Demo02 {
    public static void main(String[] args) {
        //创建一个Properties集合
        Properties properties = new Properties();

        //Object setProperties(String key,String value)   设置集合的键和值,都是String类型,底层调用Hashtable方法put
        properties.setProperty("yang","18");
        properties.setProperty("yang2","188");
        properties.setProperty("yang3","1888");
        /*这个方法保证了参数是String类型
            public synchronized Object setProperty(String key, String value) {return put(key, value);}
            public synchronized V put(K key, V value) {
              // Make sure the value is not null
              f (value == null) {
              throw new NullPointerException();
              }
         */

        //String getProperties(String key)                使用此属性列表中指定的键搜索属性
        System.out.println(properties.getProperty("yang2"));
        System.out.println(properties.getProperty("yang3"));
        System.out.println(properties.getProperty("yang"));

        //Set<String> stringPropertyNames()               从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set<String> strings = properties.stringPropertyNames();
        for (String s:strings){
            System.out.println(s);
        }
    }
}

Properties类和IO流相结合的方法

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

/*Properties和IO流相结合的方法
void load(InputStream inStream):从输入字节流读取属性列表(键和元素对)
void load(Reader reader):从输入字节流读取属性列表(键和元素对)
void store(OutputStream out,String comments):将此属性列表(键和元素对)写入此Properties表中,以适合使用load(InputStream)方法的格式写入输出字节流
void store(Writer writer,String comments):将此属性列表(键和元素对)写入此Properties表中,以适合使用load(Reader)方法的格式写入输出字符流
 */
public class Demo03 {
    public static void main(String[] args) throws IOException{
        //用字符流进行演示
        //将集合加载到文件中
        myStore();
        //将文件的内容加载到properties集合
        myLoad();
    }

    private static void myLoad() throws IOException {
        Properties properties = new Properties();
        FileReader fileReader = new FileReader("JAVA基础语法\\fw.txt");
        properties.load(fileReader);
        System.out.println(properties);
    }

    private static void myStore() throws IOException {
        Properties properties = new Properties();
        properties.setProperty("oo1","001");
        properties.setProperty("oo2","002");
        properties.setProperty("oo3","003");
        FileWriter fileWriter = new FileWriter("JAVA基础语法\\fw.txt");
        properties.store(fileWriter,"集合到文件");
    }
}

案例:猜数字小游戏

每次玩游戏的时候将游戏次数增加一,把游戏次数保存到本地文件,当游戏次数到达三时则需要充值。需要本地有一个game.txt

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

//案例:猜数字小游戏
public class Test {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        FileReader fileReader = new FileReader("JAVA基础语法\\game.txt");
        properties.load(fileReader);
        fileReader.close();
        String count = properties.getProperty("count");
        int i = Integer.parseInt(count);
        if (i>=3){
            System.out.println("试玩已结束,请充值");
        }else{
            Game.start();
            i++;
            FileWriter fileWriter = new FileWriter("JAVA基础语法\\game.txt");
            properties.setProperty("count",String.valueOf(i));
            properties.store(fileWriter,null);
        }
    }
}

游戏类

import java.util.Random;
import java.util.Scanner;

//猜数字游戏的类
public class Game {
    public Game() {
    }
    public static void start(){
        Random random = new Random();
        int ans=random.nextInt(100)+1;
        System.out.println("请输入1-100内的数字");
        Scanner scanner=new Scanner(System.in);

        while (true){
            int a=scanner.nextInt();
            if (a>ans){
                System.out.println("你输入的数字大了!");
            }else if (a<ans){
                System.out.println("你输入的数字小了!");
            }else {
                System.out.println("恭喜你猜对了!");
                break;
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值