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 返回一組鍵值對 |
注意事項
- 因爲是字節流輸出,所以存儲的文件內容中文會用
ASCII碼
替代。 new FileOutputStream
中第二個參數true
是追加,false
是覆蓋。- 文本內容排布規律還沒搞清楚,反正每次不同內容長度都不一樣。
- 輸出的同時會附加當前時間如:
#Wed Apr 29 00:28:48 CST 2020
。 - 如果匹配不到鍵,則會返回
null
。 - 如果有多個重複的鍵,默認倒着讀文件,只對第一個讀到的鍵有效。
代碼
完整代碼
看準了代碼整體結構,再接着往下看。
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);
推薦相關文章
====================================================
看完上面這些,估計你也會了,不會就繼續學,我也在學,太菜了我。