十二、Map和Properties

1、Map接口:

01- Map接口.png

Map接口有三个比较重要的实现类,分别是HashMap、TreeMap和HashTable。

TreeMap是有序的,HashMap和HashTable是无序的。
Hashtable的方法是同步的,HashMap的方法不是同步的。这是两者最主要的区别。

这就意味着:

  • Hashtable是线程安全的,HashMap不是线程安全的。
  • HashMap效率较高,Hashtable效率较低。
  • Hashtable不允许null值,HashMap允许null值(key和value都允许)
  • 父类不同:Hashtable的父类是Dictionary,HashMap的父类是AbstractMap

2、Properties属性集:

java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>

Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。

Properties集合是一个唯一和IO流相结合的集合。
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用。

属性列表中每个键及其对应值都是一个字符串。
Properties集合是一个双列集合,key和value默认都是字符串。

(1)构造方法

  • public Properties() :创建一个空的属性列表。

(2)基本的存储方法

  • public Object setProperty(String key, String value): 保存一对属性。
  • public String getProperty(String key):使用此属性列表中指定的键搜索属性值。
  • public Set<String> stringPropertyNames():所有键的名称的集合。

(3)与流相关的方法
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储:
public void store(OutputStream out, String comments)
public void store(Writer writer, String comments)

  • 参数:
    OutputStream out:字节输出流,不能写入中文。
    Writer writer:字符输出流,可以写中文。
    String comments:注释,用来解释说明保存的文件是做什么用的,不能使用中文,会产生乱码,默认是Unicode编码,一般使用""空字符串。

  • 使用步骤:
    1.创建Properties集合对象,添加数据。
    2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地。
    3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。
    4.释放资源。

  • 代码实现:

public void testProperties() throws IOException {
        //1.创建Properties集合对象,添加数据
        Properties prop = new Properties();
        prop.setProperty("赵丽颖", "168");
        prop.setProperty("迪丽热巴", "165");
        prop.setProperty("古力娜扎", "160");
        //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
        String pathName = "E:\\javatest文件\\prop.txt";
        FileWriter fw = new FileWriter(pathName);
        //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
        prop.store(fw, "save data");
        //4.释放资源
        fw.close();
    }

可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对)读取到集合中:
public void load(InputStream inStream)
public void load(Reader reader)

  • 参数:
    InputStream inStream:字节输入流,不能读取含有中文的键值对。
    Reader reader:字符输入流,能读取含有中文的键值对。
  • 使用步骤:
    1.创建Properties集合对象;
    2.使用Properties集合对象中的方法load读取保存键值对的文件;
    3.遍历Properties集合。
  • 注意:
    1.存储键值对的文件中,键与值默认的连接符号可以使用=和空格(其他符号)
    2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
    3.存储键值对的文件中,键与值默认都是字符串,不用再加引号
  • 代码实现:
public void testProperties() throws IOException {
        //1.创建Properties集合对象
        Properties prop = new Properties();
        //2.使用Properties集合对象中的方法load读取保存键值对的文件
        String pathName = "E:\\javatest文件\\prop.txt";
        prop.load(new FileReader(pathName));
        //prop.load(new FileInputStream(pathName));

        //使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中
        Set<String> set = prop.stringPropertyNames();
        //3.遍历Properties集合
        for (String key : set) {
            String value = prop.getProperty(key);
            System.out.println(key + "=" + value);
        }
    }

3、利用Properties读取配置文件的几种方式:

(1)基于ClassLoder读取配置文件

注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。

    Properties properties = new Properties();
    // 使用ClassLoader加载properties配置文件生成对应的输入流
    InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");
    // 使用properties对象加载输入流
    properties.load(in);
    //获取key对应的value值
    properties.getProperty(String key);

(2)基于InputStream读取配置文件

注意:该方式的优点在于可以读取任意路径下的配置文件。

    Properties properties = new Properties();
    // 使用InPutStream流读取properties文件
     BufferedReader bufferedReader = new BufferedReader(new FileReader("D:/config.properties"));
     properties.load(bufferedReader);
     // 获取key对应的value值
     properties.getProperty(String key);

(3)通过 java.util.ResourceBundle 类来读取

①通过 ResourceBundle.getBundle() 静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

    //config为属性文件名,放在包com.test.config下,如果是放在src下,直接用config即可  
    ResourceBundle resource = ResourceBundle.getBundle("com/test/config/config");
    String key = resource.getString("keyWord"); 

②从 InputStream 中读取,获取 InputStream 的方法和上面一样。

    ResourceBundle resource = new PropertyResourceBundle(inStream);
    String key = resource.getString("keyWord")

小贴士:
在使用中遇到的最大的问题可能是配置文件的路径问题,
如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取)或com/test/config/config(通过ResourceBundle来获取);
属性文件在src根目录下,则直接使用config.properties或config即可.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。