zoukankan      html  css  js  c++  java
  • 111.Java中Properties

    Properties

    可以和流相关联的集合对象Properties.

    Map

    |--Hashtable

    |--Properties

    Properties:该集合不需要泛型,因为该集合中的键值对都是String类型。

    1,存入键值对:setProperty(key,value);

    2,获取指定键对应的值:value getProperty(key);

    3,获取集合中所有键元素:

    Enumeration  propertyNames();

    在jdk1.6版本给该类提供一个新的方法。

    Set<String> stringPropertyNames();

    4,列出该集合中的所有键值对,可以通过参数打印流指定列出到的目的地。

    list(PrintStream);

    list(PrintWriter);

    例:list(System.out):将集合中的键值对打印到控制台。

    list(new PrintStream("prop.txt")):将集合中的键值对存储到prop.txt文件中。

    5,可以将流中的规则数据加载进行集合,并称为键值对。

    load(InputStream):

    jdk1.6版本。提供了新的方法。

    load(Reader):

    注意:流中的数据要是"键=值" 的规则数据。

    6,可以将集合中的数据进行指定目的的存储。

    store(OutputStram,String comment)方法。

    jdk1.6版本。提供了新的方法。

    store(Writer ,String comment):

    使用该方法存储时,会带着当时存储的时间。

    注意:

    Properties只加载key=value这样的键值对,与文件名无关,注释使用#

    练习:记录一个程序运行的次数,当满足指定次数时,该程序就不可以再继续运行了。

    通常可用于软件使用次数的限定。

    public static void sysPropList() throws IOException {
            Properties prop = System.getProperties();
    
            // prop.list(System.out);// 目的是控制台。
            // 需求是:将jvm的属性信息存储到一个文件中。
            prop.list(new PrintStream("java.txt"));
        }
    
        public static void sysProp() {
            Properties prop = System.getProperties();
    
            Set<String> keys = prop.stringPropertyNames();
    
            for (String key : keys) {
                System.out.println(key + ":" + prop.getProperty(key));
            }
        }

    Properties类与配置文件

    Map

       |--Hashtable

          |--Properties

    注意:是一个Map集合,该集合中的键值对都是字符串。该集合通常用于对键值对形式的配置文件进行操作.

    配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后更改,该文件称之为配置文件。

    优势: 提高代码的维护性。

    Properties:  该类是一个Map的子类,提供了可以快速操作配置文件的方法

    load()  :    将文件设备数据装载为Map集合数据

    get(key):    获取Map中的数据

    getProperty()获取Map中的数据特有方法

    案例:

    /*
         * 将配置文件中的数据通过流加载到集合中。
         */
        public static void loadFile() throws IOException {
            // 1,创建Properties(Map)对象
            Properties prop = new Properties();
    
            // 2.使用流加载配置文件。
            FileInputStream fis = new FileInputStream("c:\qq.txt");
    
            // 3。使用Properties 对象的load方法将流中数据加载到集合中。
            prop.load(fis);
    
            // 遍历该集合
            Set<Entry<Object, Object>> entrySet = prop.entrySet();
            Iterator<Entry<Object, Object>> it = entrySet.iterator();
            while (it.hasNext()) {
                Entry<Object, Object> next = it.next();
                Object key = next.getKey();
                Object value = next.getValue();
            }
            // 通过键获取指定的值
            Object object = prop.get("jack");
            System.out.println(object);
    
            // 通过键修改值
            prop.setProperty("jack", "888888");
    
            // 将集合中的数据写入到配置文件中。
            FileOutputStream fos = new FileOutputStream("c:\qq.txt");
    
            // 注释:
            prop.store(fos, "yes,qq");
    
            fos.close();
            fis.close();
    
        }

    获取记录程序运行次数:

    public class Demo6 {
        public static void main(String[] args) throws IOException {
            int count = 0;
            Properties pro = new Properties();
    
            File file = new File("c:\count.ini");
            FileInputStream fis = null;
            if (!file.exists()) {
                file.createNewFile();
            }
            fis = new FileInputStream(file);
            pro.load(fis);
            String str = pro.getProperty("count");
            if (str != null) {
                count = Integer.parseInt(str);
            }
            if (count == 3) {
                System.out.println("使用次数已到,请付费");
                System.exit(0);
            }
    
            count++;
            System.out.println("欢迎使用本软件" + "你已经使用了:" + count + " 次");
    
            pro.setProperty("count", count + "");
            FileOutputStream fos = new FileOutputStream(new File("c:\count.ini"));
            pro.store(fos, "请保护知识产权");
    
            fis.close();
            fos.close();
    
        }
    }
    author@nohert
  • 相关阅读:
    excel unixtime与北京时间互转
    vim的漫漫长征路
    const常量
    第一章:绪论
    2.4奇偶校验
    2.3数据校验的基本原理
    2.2定点与浮点数据表示
    2.1机器数及其特点
    1.2计算机系统性能评价
    冯诺依曼结构原理及层次分析
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13665155.html
Copyright © 2011-2022 走看看