• 黑马程序员JAVA高级视频_IO输入与输出20天7(Properties练习)


    package string.itcastio;
    
    import java.io.*;
    import java.util.*;
    
    /*
     * Properties是hashTable的子类
     * 也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串
     * 是集合中和IO技术相结合的集合容器
     * 该对象的特点:可以用于键值对形式的配置文件。
     * 那么在加载数据时,需要数据有固定格式:键=值
     */
    public class PropertiesDemo {
        public static void main(String[] args) throws IOException {
            // setAndget();
            // method_1();
            //loadDemo();
            runCount();
        }
    
        /**
         * 设置与获取元素
         */
        public static void setAndget() {
            Properties properties = new Properties();
            properties.setProperty("zhangsan", "001");
            properties.setProperty("lisi", "002");
            System.out.println(properties);
            // getProperty根据指定key获取value
            String value = properties.getProperty("lisi");
            System.out.println(value);
            // setProperty修改指定key的value
            properties.setProperty("lisi", "007");
            // stringPropertyNames从java1.6开始才出现的
            for (String name : properties.stringPropertyNames()) {
                System.out.println("key:" + name + "-value:" + properties.getProperty(name));
            }
        }
    
        /**
         * 如何将流中的数据存储到集合中 想要将info.txt中的键值数据存到集合中进行操作 1,用一个流和info.txt文件关联。
         * 2,读取一行数据,将该行数据用“=”号经行分割 3,等号左边作为键,右边作为值。存入到Properties 集合中去
         */
        public static void method_1() throws IOException {
            FileReader fr = new FileReader("info.txt");
            BufferedReader br = new BufferedReader(fr);
            Properties properties = new Properties();
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] keyvalue = line.split("=");
                properties.setProperty(keyvalue[0], keyvalue[1]);
            }
            System.out.println(properties);
            br.close();
        }
    
        /**
         * 使用load加载txt文件内容到Properties
         * 
         * @throws IOException
         */
        public static void loadDemo() throws IOException {
            File file = new File("info.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            Properties properties = new Properties();
            properties.load(new FileInputStream(file));
    
            properties.setProperty("lisi", "ii");
            FileOutputStream fos = new FileOutputStream(file);
            properties.store(fos, "xixi");
            System.out.println(properties);
            fos.close();
        }
    
        /**
         * 模拟软件使用次数
         * 
         * @throws IOException
         */
        public static void runCount() throws IOException {
            File file = new File("count.ini");
            if (!file.exists()) {
                file.createNewFile();
            }
            Properties properties = new Properties();
            FileInputStream fis = new FileInputStream(file);
            properties.load(fis);
            int count = 0;
            String value = properties.getProperty("time");
            if (value != null) {
                //System.out.println(value);
                count = Integer.parseInt(value);
                if (count >= 5) {
                    System.out.println("使用已经到期了!请续费..");
                    return;
                }
            }
            count++;
            //System.out.println(count);
            properties.setProperty("time", "" + count);
            FileOutputStream fos = new FileOutputStream(file);
            properties.store(fos, "");
            fos.close();
            fis.close();
    
        }
    }
  • 相关阅读:
    Java在ACM中的应用
    acm->stl
    残缺棋盘--状压DP
    EOJ Monthly 2019.3 A
    【CF1141E】Superhero Battle
    AtCoder Grant Contest 10.F 博弈
    莫比乌斯反演总结
    P2257 YY的GCD
    BZOJ1011 莫比乌斯反演(基础题
    HDU1695 莫比乌斯反演
  • 原文地址:https://www.cnblogs.com/guwenren/p/2980805.html
Copyright © 2020-2023  润新知