zoukankan      html  css  js  c++  java
  • 黑马程序员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();
    
        }
    }
  • 相关阅读:
    使用微软WPF技术开发产品优势究竟在那里
    北京地铁的众生相,发上来和大家分享(北京白领生活真实写照)
    长期提供WindowsPhone7培训 & HTML5培训 & Silverlight培训 & WPF培训
    Silverlight中开发和设计人员的合作
    Silverlight 动态调用 WebService
    (学)Lazarus 在 WinCE 下如何取消标题栏上的 OK 或 X 按钮
    (学)新版动态表单研发,阶段成果4
    (学)新版动态表单研发,阶段成果5
    (原)Lazarus下WinCE终端、服务器心跳监控(Ping)源码
    (原)Lazarus 在 Thread 中使用 SQLQuery 查询Oracle 会出现异常
  • 原文地址:https://www.cnblogs.com/guwenren/p/2980805.html
Copyright © 2011-2022 走看看