Properties

Properties,第1张

Properties

Demo01: 

 

public class PropertiesDemo01 {
    public static void main(String[] args) {
        Properties pt = new Properties();
        pt.put("ggg",12);
        pt.put("xxx",13);
        pt.put("ccc",14);
        Set> entries = pt.entrySet();
        for (Map.Entry tt:entries){
            Object value = tt.getValue();
            Object key = tt.getKey();
            System.out.println(key+","+value);
        }

    }
Demo02:Properties 的特有方法

​​​​​​​

 

 Demo03:

package PropertiesDemo;

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

public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
//        proStore();
        proLoad();
    }

    private static void proLoad() throws IOException {
        Properties pp = new Properties();
        FileReader fr = new FileReader("/Users/verle/DeskTop/Properties.txt");
        pp.load(fr);
        fr.close();
        System.out.println(pp);
        Set strings = pp.stringPropertyNames();
        for (String tt:strings){
            String value = pp.getProperty(tt);
            System.out.println(tt+","+value);
        }
    }

    private static void proStore() throws IOException {
        Properties pp = new Properties();
        pp.setProperty("xx","22");
        pp.setProperty("cc","23");
        pp.setProperty("vv","24");
        FileWriter fw = new FileWriter("/Users/verle/DeskTop/Properties.txt");
        pp.store(fw,null);
        fw.close();
    }
}

Demo04:

package PropertiesDemo;

import java.io.*;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo04 {
    public static void main(String[] args) throws IOException {
        Properties pp = new Properties();
        FileReader fr = new FileReader("/Users/verle/DeskTop/gg.txt");
        pp.load(fr);
        fr.close();
        String count = pp.getProperty("count");
        int i = Integer.parseInt(count);
        if (i>=3){
            System.out.println("游戏试玩已经结束,请充值!");
        }else {
            GussNumber.start();
            i++;
            Properties p2 = new Properties();
            FileWriter f2 = new FileWriter("/Users/verle/DeskTop/gg.txt");
            p2.setProperty("count",String.valueOf(i));
            p2.store(f2,null);
            f2.close();
        }


    }
}
package PropertiesDemo;

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

public class GussNumber {
    public static void start() {
        Random random = new Random();
        int i = random.nextInt(100) + 1;
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你猜的数字:");
            int number = scanner.nextInt();
            if (number > i) {
                System.out.println("你猜的数字" + number + "大了");
            } else if (number < i) {
                System.out.println("你猜的数字" + number + "小了");
            } else {
                System.out.println("恭喜你猜对了!!");
                break;
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5611878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存