java对配置文件properties的操作

痛定思痛。 2023-08-22 06:37 242阅读 0赞

1.读取配置文件的键值对,转为Properties对象;将Properties(键值对)对象写入到指定文件。

  1. package com.ricoh.rapp.ezcx.admintoolweb.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.Properties;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. public class PropertiesFileHandle {
  12. private static Logger logger = LoggerFactory.getLogger(PropertiesFileHandle.class);
  13. public static Properties readProperties(String filePath) {
  14. String realPath = FileUtil.getEzChargerInstallPath() + filePath;
  15. Properties props = new Properties();
  16. File configFile = new File(realPath);
  17. logger.debug("#configFile: " + configFile.getAbsolutePath());
  18. InputStream fis = null;
  19. try {
  20. fis = new FileInputStream(configFile);
  21. props.load(fis);
  22. } catch (IOException e) {
  23. logger.error("readProperties failed in" + realPath + ". "+ e.toString());
  24. return null;
  25. } finally {
  26. try {
  27. if (fis != null) {
  28. fis.close();
  29. }
  30. } catch (Exception e) {
  31. logger.debug("readProperties close file failed." + e.toString());
  32. }
  33. }
  34. return props;
  35. }
  36. public static boolean writeProperties(String filePath, Properties prop) {
  37. String realPath = FileUtil.getEzChargerInstallPath() + filePath;
  38. File configFile = new File(realPath);
  39. if(!configFile.exists()) {
  40. configFile.getParentFile().mkdirs();
  41. try {
  42. configFile.createNewFile();
  43. } catch (IOException e) {
  44. logger.error("PropertiesFileHandle.writeProperties failed. because create file[" + realPath
  45. + "]. is IOException:"+ e.getMessage());
  46. e.printStackTrace();
  47. return false;
  48. }
  49. }
  50. InputStream fis = null;
  51. OutputStream fos = null;
  52. try {
  53. fos = new FileOutputStream(configFile);
  54. prop.store(fos, "");
  55. } catch (Exception e) {
  56. logger.error("WriteProperties failed in" + realPath + ". "+ e.toString());
  57. return false;
  58. } finally {
  59. try {
  60. if (fos != null) {
  61. fos.close();
  62. }
  63. if (fis != null) {
  64. fis.close();
  65. }
  66. } catch (Exception e) {
  67. logger.debug("writeProperties close file failed." + e.toString());
  68. }
  69. }
  70. return true;
  71. }
  72. }

2.通过输入流或者Properties对象将Properties文件的内容读取到map集合中。

  1. package com.ricoh.rapp.ezcx.edcactivation.internal;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Properties;
  8. import java.util.Map.Entry;
  9. public class PropertyUtil {
  10. public static Map<String, String> loadPropertiesFile(InputStream file) {
  11. HashMap result = new HashMap();
  12. try {
  13. Properties prop = new Properties();
  14. prop.load(file);
  15. Iterator var4 = prop.entrySet().iterator();
  16. while (var4.hasNext()) {
  17. Entry<Object, Object> entry = (Entry) var4.next();
  18. result.put((String) entry.getKey(), (String) entry.getValue());
  19. }
  20. } catch (IOException var5) {
  21. System.out.println("faild load properties file .");
  22. }
  23. return result;
  24. }
  25. public static Map<String, String> loadPropertiesFile(Properties prop) {
  26. Map<String, String> result = new HashMap();
  27. if (prop == null) {
  28. return result;
  29. } else {
  30. Iterator var5 = prop.entrySet().iterator();
  31. while (var5.hasNext()) {
  32. Entry<Object, Object> entry = (Entry) var5.next();
  33. String key = (String) entry.getKey();
  34. String value = (String) entry.getValue();
  35. if (key != null && key.length() > 0 && value != null && value.length() > 0) {
  36. result.put(key, value);
  37. }
  38. }
  39. return result;
  40. }
  41. }
  42. }

3.实例使用1和2的方式来处理Properties文件

  1. private void innitPropreties() {
  2. Map<String, String> rsiConfMap = new HashMap<>();
  3. Properties proxyProp = PropertiesFileHandle.readProperties("/conf/test.properties");
  4. if (proxyProp != null) {
  5. rsiConfMap = PropertyUtil.loadPropertiesFile(proxyProp);
  6. }else {
  7. rsiConfMap.put("key1", "value1");
  8. rsiConfMap.put("key2", "value2");
  9. Properties properties = new Properties();
  10. properties.put("key1", "value1");
  11. properties.put("key2", "value2");
  12. PropertiesFileHandle.writeProperties("/conf/test.properties", properties);
  13. }
  14. String value1= rsiConfMap.get("key1");
  15. String value2= rsiConfMap.get("key2");
  16. }

转载于:https://www.cnblogs.com/wang-liang-blogs/p/11131318.html

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读