• Java中Properties类知识的总结


    一、Properties类与配置文件

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

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

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

    二. JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:

          1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

          2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

          3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

          4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

          5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

    实例解读:

    1、将配置文件中的数据通过流加载到集合中。         

     1  /*
     2       * 将配置文件中的数据通过流加载到集合中。
     3        */
     4      public static void loadFile() throws IOException {
     5           // 1,创建Properties(Map)对象
     6           Properties prop = new Properties();
     7   
     8           // 2.使用流加载配置文件。
     9           FileInputStream fis = new FileInputStream("E:\qq.txt");
    10  
    11          // 3。使用Properties 对象的load方法将流中数据加载到集合中。
    12          prop.load(fis);
    13  
    14          // 遍历该集合
    15          Set<Entry<Object, Object>> entrySet = prop.entrySet();
    16          Iterator<Entry<Object, Object>> it = entrySet.iterator();
    17          while (it.hasNext()) {
    18              Entry<Object, Object> next = it.next();
    19              Object key = next.getKey();
    20              Object value = next.getValue();
    21          }
    22          // 通过键获取指定的值
    23          Object object = prop.get("jack");
    24          System.out.println(object);
    25  
    26          // 通过键修改值        prop.setProperty("jack", "888888");
    27  
    28          // 将集合中的数据写入到配置文件中。
    29          FileOutputStream fos = new FileOutputStream("E:\qq.txt");
    30 
    31          // 注释:
    32          prop.store(fos, "yes,qq");
    33  
    34          fos.close();
    35          fis.close();
    36  
    37      }

    获取记录程序运行次数:

     1 public class Demo2 {
     2     public static void main(String[] args) throws IOException {
     3         int count = 0;
     4         Properties pro = new Properties();
     5 
     6         File file = new File("E:\count.ini");
     7         FileInputStream fis = null;
     8         if (!file.exists()) {
     9             file.createNewFile();
    10         }
    11         fis = new FileInputStream(file);
    12         pro.load(fis);
    13         String str = pro.getProperty("count");
    14         if (str != null) {
    15             count = Integer.parseInt(str);
    16         }
    17         if (count == 3) {
    18             System.out.println("使用次数已到,请付费");
    19             System.exit(0);
    20         }
    21 
    22         count++;
    23         System.out.println("欢迎使用本软件" + "你已经使用了:" + count + " 次");
    24 
    25         pro.setProperty("count", count + "");
    26         FileOutputStream fos = new FileOutputStream(new File("E:\count.ini"));
    27         pro.store(fos, "请保护知识产权");
    28 
    29         fis.close();
    30         fos.close();
    31 
    32     }
    33 }
    额外注意:(Java代码)-----补充
    注:(错)InputStream in = getClass().getResourceAsStream("资源Name"); 
     InputStream 是接口,抽象的,又因为getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。
  • 相关阅读:
    apache2 开源协议
    zend framework入门教程
    对open页的打开页面进行刷新
    mysql -- 视图
    MySQL ---存储过程和触发器
    mysql --存储过程 select ...into
    mysql -- 存储过程,游标,错误异常处理
    mysql --存储过程 退出
    mysql -- 存储过程 in out inout
    mysql -- 死锁
  • 原文地址:https://www.cnblogs.com/lwlxqlccc/p/6148131.html
Copyright © 2020-2023  润新知