IniFileUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.miniframe.tools;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.Hashtable;
  9. public class IniFileUtil {
  10. private Hashtable<String, Object> paramTable;
  11. public IniFileUtil(String conf_filename) {
  12. try {
  13. this.loadFromFile(conf_filename);
  14. } catch (FileNotFoundException var3) {
  15. var3.printStackTrace();
  16. } catch (IOException var4) {
  17. var4.printStackTrace();
  18. }
  19. }
  20. public String getStrValue(String name) {
  21. if (this.paramTable == null) {
  22. return null;
  23. } else {
  24. Object obj = this.paramTable.get(name);
  25. if (obj == null) {
  26. return null;
  27. } else {
  28. return obj instanceof String ? (String)obj : (String)((ArrayList)obj).get(0);
  29. }
  30. }
  31. }
  32. public int getIntValue(String name, int default_value) {
  33. String szValue = this.getStrValue(name);
  34. return szValue == null ? default_value : Integer.parseInt(szValue);
  35. }
  36. public boolean getBoolValue(String name, boolean default_value) {
  37. String szValue = this.getStrValue(name);
  38. if (szValue == null) {
  39. return default_value;
  40. } else {
  41. return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") || szValue.equalsIgnoreCase("true") || szValue.equals("1");
  42. }
  43. }
  44. public String[] getValues(String name) {
  45. Object obj = this.paramTable.get(name);
  46. if (obj == null) {
  47. return null;
  48. } else {
  49. String[] values;
  50. if (obj instanceof String) {
  51. values = new String[]{(String)obj};
  52. return values;
  53. } else {
  54. Object[] objs = ((ArrayList)obj).toArray();
  55. values = new String[objs.length];
  56. System.arraycopy(objs, 0, values, 0, objs.length);
  57. return values;
  58. }
  59. }
  60. }
  61. private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException {
  62. URL url = com.miniframe.core.IniFileReader.class.getClassLoader().getResource(conf_filename);
  63. InputStreamReader ipsr = null;
  64. try {
  65. ipsr = new InputStreamReader(url.openStream(), "UTF-8");
  66. BufferedReader buffReader = new BufferedReader(ipsr);
  67. this.paramTable = new Hashtable();
  68. String line;
  69. while((line = buffReader.readLine()) != null) {
  70. line = line.trim();
  71. if (line.length() != 0 && line.charAt(0) != '#') {
  72. String[] parts = line.split("=", 2);
  73. if (parts.length == 2) {
  74. String name = parts[0].trim();
  75. String value = parts[1].trim();
  76. Object obj = this.paramTable.get(name);
  77. if (obj == null) {
  78. this.paramTable.put(name, value);
  79. } else {
  80. ArrayList valueList;
  81. if (obj instanceof String) {
  82. valueList = new ArrayList();
  83. valueList.add(obj);
  84. valueList.add(value);
  85. this.paramTable.put(name, valueList);
  86. } else {
  87. valueList = (ArrayList)obj;
  88. valueList.add(value);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. } catch (Exception var14) {
  95. var14.printStackTrace();
  96. } finally {
  97. if (ipsr != null) {
  98. ipsr.close();
  99. }
  100. }
  101. }
  102. }