Properties檔案工具類的使用--獲取所有的鍵值、刪除鍵、更新鍵等操作
阿新 • • 發佈:2018-11-03
有時候我們希望處理properties檔案,properties檔案是鍵值對的檔案形式,我們可以藉助Properties類操作。
工具類如下:(程式碼中日誌採用了slf4j日誌)
package cn.xm.exam.utils; import java.io.File; 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.Map.Entry; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 操作properties檔案的工具類(此工具類的file都是src目錄下的properties檔案,編譯之後在build目錄下) * * @author QiaoLiQiang * @time 2018年11月3日下午12:05:32 */ public class PropertiesFileUtils {private static final Logger log = LoggerFactory.getLogger(PropertiesFileUtils.class); /** * 建構函式私有化 */ private PropertiesFileUtils() { } /** * 儲存或更新properties檔案中的key * * @param fileName * @param key * @param value */ public staticvoid saveOrUpdateProperty(String fileName, String key, String value) { Properties properties = new Properties(); InputStream inputStream; OutputStream outputStream; try { String path = ResourcesUtil.class.getClassLoader().getResource(fileName).getPath(); log.debug("path -> {}", path); inputStream = new FileInputStream(new File(path)); properties.load(inputStream); properties.setProperty(key, value); // 儲存到檔案中(如果有的話會自動更新,沒有會建立) outputStream = new FileOutputStream(new File(path)); properties.store(outputStream, ""); outputStream.close(); inputStream.close(); } catch (FileNotFoundException e) { log.error("saveOrUpdateProperty error", e); } catch (IOException e) { log.error("saveOrUpdateProperty error", e); } } /** * 獲取Properties * * @param fileName * @param key * @return */ public static String getPropertyValue(String fileName, String key) { Properties properties = new Properties(); InputStream inputStream; try { String path = PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath(); log.info("path -> {}", path); inputStream = new FileInputStream(new File(path)); properties.load(inputStream); for (Entry<Object, Object> entry : properties.entrySet()) { log.info("key -> {}, value -> {}", entry.getKey(), entry.getValue()); } // 儲存到檔案中(如果有的話會自動更新,沒有會建立) inputStream.close(); } catch (FileNotFoundException e) { log.error("saveOrUpdateProperty error", e); } catch (IOException e) { log.error("saveOrUpdateProperty error", e); } return ""; } /** * 獲取Properties * * @param fileName * @return */ public static Properties getProperties(String fileName) { Properties properties = new Properties(); InputStream inputStream; try { String path = PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath(); log.info("path -> {}", path); inputStream = new FileInputStream(new File(path)); properties.load(inputStream); inputStream.close(); } catch (FileNotFoundException e) { log.error("saveOrUpdateProperty error", e); } catch (IOException e) { log.error("saveOrUpdateProperty error", e); } return properties; } /** * 獲取Properties * * @param fileName * @return */ public static Properties removeProperty(String fileName, String key) { Properties properties = new Properties(); InputStream inputStream; OutputStream outputStream; try { String path = PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath(); log.info("path -> {}", path); inputStream = new FileInputStream(new File(path)); properties.load(inputStream); log.info("properties -> {}", properties); if (properties != null && properties.containsKey(key)) { log.info("remove key:{}", key); properties.remove(key); } // 儲存到檔案中(將properties儲存到檔案) outputStream = new FileOutputStream(new File(path)); properties.store(outputStream, ""); outputStream.close(); inputStream.close(); } catch (FileNotFoundException e) { log.error("saveOrUpdateProperty error", e); } catch (IOException e) { log.error("saveOrUpdateProperty error", e); } return properties; } public static void main(String[] args) { // 儲存三個 最後一個相當於更新 PropertiesFileUtils.saveOrUpdateProperty("settings.properties", "a", "aaa"); PropertiesFileUtils.saveOrUpdateProperty("settings.properties", "b", "bbb"); PropertiesFileUtils.saveOrUpdateProperty("settings.properties", "c", "ccc"); PropertiesFileUtils.saveOrUpdateProperty("settings.properties", "a", "AAA"); // 獲取所有的properties Properties properties = PropertiesFileUtils.getProperties("settings.properties"); System.out.println(properties); // 刪除a PropertiesFileUtils.removeProperty("settings.properties", "a"); // 獲取所有的properties Properties properties1 = PropertiesFileUtils.getProperties("settings.properties"); System.out.println(properties1); } }
結果:
{b=bbb, a=AAA, c=ccc}
{b=bbb, c=ccc}
解釋:
Properties是繼承了HashTable的一個普通類,所以我們可以簡單的認為操作Properties就是在操作HashTable。
public class Properties extends Hashtable<Object,Object> { private static final long serialVersionUID = 4112578634029874840L; protected Properties defaults; 。。。 }
由於HasTable鍵不可以重複,所以我們在saveOrUpdateProperty中直接setProperty的時候如果沒有key會建立key,如果key存在會覆蓋原來的值。
properties.load(inputStream);是將properties檔案中的key=value的資料載入到properties中;
properties.store(outputStream, "");是將properties儲存到一個檔案中。