通常將系統相關的配置和屬性,例如數據庫、緩存等寫在properties文件中,方便管理和維護。參考JDK7中的Properties類,學著進行了一些對properties文件的操作,總結一下,留存以后查看。
Properties 類說明
- Properties 類表示了一個持久的屬性集。
- Properties可保存在流中或從流中加載。
- 屬性列表中每個鍵及其對應值都是一個字符串。
- Properties繼承于Hashtable。
文件所在目錄:
- /config/config.properties
- /src/com/OperateProperties.java
properties件內容
http.host=127.0.0.1
http.port=8080
方法說明
- 讀取properties文件
InputStream in = new FileInputStream(filePath);
Properties properties = new Properties();
properties.load(in);
- 根據key獲取value
String host = properties.getProperty("http.host");
- 獲取所有內容
Enumeration en = properties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = properties.getProperty(key);
}
- 修改value
properties.setProperty("http.host", "192.168.1.1");
// 輸出
OutputStream fos = new FileOutputStream(filePath);
// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
properties.store(fos, "add key:'uri'");
代碼
package com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
/**
* Properties文件操作,方法
*
* @author notNine
* @since 1.0
* @version 2016.11.8
*/
public class OperateProperties {
// 項目路徑
public static final String root = System.getProperty("user.dir");
// properties文件路徑
public static final String filePath = root + "/config/config.properties";
public static void main(String[] args) {
// 根據key讀取properties的value
readProperties();
// 讀取properties所有內容
// readAllProperties();
// 寫入properties內容
// writeProperties();
}
/**
* 讀取properties內容,根據key獲取value
*/
public static void readProperties() {
try {
// 讀取properties文件
InputStream in = new FileInputStream(filePath);
Properties properties = new Properties();
properties.load(in);
// 根據key獲取value
String host = properties.getProperty("http.host");
String port = properties.getProperty("http.port");
// 打印
System.out.println("host:" + host + ",port:" + port);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 讀取properties內容,所有內容
*/
public static void readAllProperties() {
try {
// 讀取properties文件
InputStream in = new FileInputStream(filePath);
Properties properties = new Properties();
properties.load(in);
// 獲取所有內容
Enumeration en = properties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = properties.getProperty(key);
// 打印
System.out.println(key + Property);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 寫入properties內容
*/
public static void writeProperties() {
try {
// 讀取properties文件
InputStream in = new FileInputStream(filePath);
Properties properties = new Properties();
properties.load(in);
// 修改host
properties.setProperty("http.host", "192.168.1.1");
// 原文件中沒有uri,會新增加key和value,但是沒有寫入保存到文件中
properties.setProperty("http.uri", "/info");
// 輸出
OutputStream fos = new FileOutputStream(filePath);
// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
properties.store(fos, "add key:'uri'");
// 根據key獲取value
String host = properties.getProperty("http.host");
String port = properties.getProperty("http.port");
String uri = properties.getProperty("http.uri");
// 打印
System.out.println("host:" + host + ",port:" + port + ",uri:" + uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行結果
host:127.0.0.1,port:8080