Java中简单地用Properties类读写键值对

前言

之前对这键值对仰慕已久,但一直无从下手,终于,在机缘巧合下,我得以逐步认识键值对。

至于为什么要把键值对和文件关联在一起,因为他们本来就是可以联系的,而且我也需要他们的关联性。

没错,就是因为需要写一个Android Application,具体的学习过程,我后面也会逐步写出来。

当然,使用Properties只是方法之一,但我现在急着用,就先凑合着吧,其他的方法,日后再补充,一定。

介绍Properties类

Properties 继承于 Hashtable,表示一个持久的属性集。属性列表中每个键及其对应值都是一个字符串。介绍来源:菜鸟教程 - Properties

为什么介绍这么短?

反正你也没兴趣看,网上大把多这种介绍,讲实用点。

Properties类常用的方法

方法 说明
getProperty(String key) 搜寻特定键的值,默认是null
setProperty(String key, String value) 把键值对先传到properties
load(InputStream inStream) 按字节流读取键值对到properties
store(OutputStream out, String comments) 按字节流输出,并输出简介
stringPropertyNames() properties返回一组键值对

注意事项

  1. 因为是字节流输出,所以存储的文件内容中文会用ASCII码替代。
  2. new FileOutputStream中第二个参数true是追加,false是覆盖。
  3. 文本内容排布规律还没搞清楚,反正每次不同内容长度都不一样。
  4. 输出的同时会附加当前时间如:#Wed Apr 29 00:28:48 CST 2020
  5. 如果匹配不到键,则会返回null
  6. 如果有多个重复的键,默认倒着读文件,只对第一个读到的键有效。

代码

完整代码

看准了代码整体结构,再接着往下看。

package priv.thdmi.app.keyandvalues;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/**
 * @author THDMI
 */
public class StartMain {

    public static void main(String[] args) throws IOException {

        // Write
        Properties properties = new Properties();

        properties.setProperty("userPass", "12345_abc");
        properties.setProperty("studentName", "李金鍪");
        properties.setProperty("studentID", "2018070230421");
        properties.setProperty("grade", "2018");
        properties.setProperty("major", "物联网工程");
        properties.setProperty("class", "4");
        Set<String> set = properties.stringPropertyNames();
        for (String key : set) {
            System.out.println(key + " : " + properties.getProperty(key));
        }

        FileOutputStream fileOutputStream = new FileOutputStream("userInformation.ini",false);
        properties.store(fileOutputStream, "账户相关信息");
        fileOutputStream.close();

        // Read
        FileInputStream fileInputStream = new FileInputStream("userInformation.ini");
        properties.load(fileInputStream);
        fileInputStream.close();
        String getUserName = properties.getProperty("userNames");
        String getStudentName = properties.getProperty("studentName");
        System.out.println("UserName: " + getUserName);
        System.out.println("StudentName: " + getStudentName);
    }
}

执行结果

输出文件userInformation.ini

输出的文件

输出控制台信息

控制台输出的信息

结果分析

如何存放键值对的?

Properties properties = new Properties();

properties.setProperty("userName", "sks828");
properties.setProperty("userPass", "12345_abc");
......

对文件操作是哪一步?

字节流写文件:

FileOutputStream fileOutputStream = new FileOutputStream("userInformation.ini",false);
properties.store(fileOutputStream, "账户相关信息");
fileOutputStream.close();

字节流读文件:

FileInputStream fileInputStream = new FileInputStream("userInformation.ini");
properties.load(fileInputStream);
fileInputStream.close();

控制台是怎么输出键 : 值列表的?

这个是从源头获取:

Set<String> set = properties.stringPropertyNames();
for (String key : set) {
	System.out.println(key + " : " + properties.getProperty(key));
}

这个是从文件中读取:

properties.load(fileInputStream);

String getUserName = properties.getProperty("userName");
String getStudentName = properties.getProperty("studentName");
System.out.println("UserName: " + getUserName);
System.out.println("StudentName: " + getStudentName);

推荐相关文章

  1. 菜鸟教程 - Java Properties类
  2. Java中的Properties类详解
  3. Java 读写键值对
  4. Java键值对的使用

====================================================

看完上面这些,估计你也会了,不会就继续学,我也在学,太菜了我。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章