zoukankan      html  css  js  c++  java
  • Java中Properties类的使用

      1.properties介绍

      java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties

    文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。该类是是HashTable的子类因此Map结合的基本使用方法它都有,但是我们需要的是下面的额:

      2.特殊方法

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

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

        c. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。只不过是将put(k KEY,V value) 通过显式的指定为String 类型保证了只可以接受String类型的参数,在实际开发时,我们也经常需要使用这种方法,来包装一个父类的方法。

        d. public void store(Writer writer, String comments)  与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

        e.public Set<String> stringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串,主要是用于遍历

    //---------------------下面直接给出一个与IO流结合的demo,里面的解释已经很详细了。。。。。

     1 import java.io.FileReader;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 import java.io.Reader;
     5 import java.io.Writer;
     6 import java.util.Properties;
     7 
     8 /*
     9  * 这里的集合必须是Properties
    10  * public void load(Reader reader) : 把文件中的数据读取到集合中
    11  * public void store(Writer writer, String comments) : 把集合中的数据存储到文件
    12  * 
    13  * 作用实现游戏进度的保存于加载
    14  */
    15 public class PropertiesDemo1 {
    16     public static void main(String[] args) throws IOException {
    17         // myLoad();  // 将数据加载到集合中,load加载
    18         myStore();    // 将集合中的数据存储到文件中, store写入
    19     }
    20 
    21     private static void myStore() throws IOException {
    22         // 创建集合对象
    23         Properties prop = new Properties();
    24         prop.setProperty("jav", "1");
    25         prop.setProperty("c++", "2");
    26         prop.setProperty("jvm", "3");
    27         
    28         // public void store(Writer writer, String comments) : 把集合中的数据存储到文件
    29         Writer w = new FileWriter("name.txt");
    30         prop.store(w, null);
    31         w.close();
    32     }
    33 
    34     private static void myLoad() throws IOException {
    35         // 创建集合对象
    36         Properties prop = new Properties();
    37 
    38         // public void load(Reader reader) : 把文件中的数据读取到集合中
    39         // 而且这个文件必须是键值对的形式!此处有格式要求
    40         Reader r = new FileReader("a.txt");
    41         prop.load(r);
    42         r.close();
    43 
    44         System.out.println(prop);
    45     }
    46 }

    //---------------

    //下面的例子使用的文件就是上面demo创建的a.txt文件。自己在eclipse中创建文件即可在里面下上c=1换行写c++=2换行写java=3欧了。

     1 import java.io.FileReader;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 import java.io.Reader;
     5 import java.io.Writer;
     6 import java.util.Properties;
     7 import java.util.Set;
     8 
     9 /*
    10  * 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
    11  * 请写一个程序判断是否有“c++”这样的键存在,如果有就改变其实为”100”
    12  * 
    13  * 分析:
    14  *         A:把文件中的数据加载到集合中
    15  *         B:遍历集合,获取得到每一个键
    16  *         C:判断键是否有为"c++"的,如果有就修改其值为"100"
    17  *         D:把集合中的数据重新存储到文件中
    18  */
    19 public class PropertiesDemo2 {
    20     public static void main(String[] args) throws IOException {
    21         // 创建集合对象
    22         Properties prop = new Properties();
    23         // 读取文件到集合
    24         Reader r = new FileReader("name.txt");
    25         prop.load(r);
    26         r.close();
    27         // 遍历获取键和值
    28         Set<String> set = prop.stringPropertyNames(); // 获取键的集合
    29         for (String key : set) {
    30             if (key.equals("c++")) {
    31                 String value = prop.getProperty(key);
    32                 prop.setProperty(key, "100"); // 把指定键设置为100
    33                 //由于键是不可以重复的因此修改一次即可!!
    34                 break;
    35             }
    36         }
    37 
    38         // 将修改后的集合元素重新写入到文件中
    39         Writer w = new FileWriter("name.txt");
    40         prop.store(w, null);
    41         w.close();
    42     }
    43 }
  • 相关阅读:
    人脸旋转 《转》
    深度学习如何入门?<知乎>
    openGL-------------别人的博客
    如何实现视频的快进快退功能(转)
    MFC+OpenGL基础绘制<转>
    野蒜
    人脸识别68个点<转>
    cv::circle《转》
    HTML5 Canvas之猜数字游戏
    LIRe 源代码分析 3:基本接口(ImageSearcher)
  • 原文地址:https://www.cnblogs.com/fuck1/p/5462151.html
Copyright © 2011-2022 走看看