C.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package com.rancedxk.monitor.utils;
  2. import org.apache.log4j.Logger;
  3. import java.io.*;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. /**
  7. * 配置文件读取工具类
  8. */
  9. public class C {
  10. private static Logger logger = Logger.getLogger(C.class);
  11. //配置文件路径
  12. private static String filePath = null;
  13. //配置项
  14. private static ConfigBuffer params = null;
  15. public static void u(String filePath){
  16. C.filePath = filePath;
  17. try {
  18. params = ConfigBuffer.load(filePath);
  19. } catch (Exception e) {
  20. logger.error("读取配置文件失败:",e);
  21. }
  22. }
  23. private static ConfigBuffer getProp() {
  24. if (params == null) {
  25. throw new IllegalStateException("未加载配置文件");
  26. }
  27. return params;
  28. }
  29. /**
  30. * 重新设置配置信息<br/>
  31. * 注:此方法不向配置文件中物理输出,如果需要物理保存,请继续调用saveConfig方法
  32. * @param key 键
  33. * @param value 值
  34. */
  35. public static void s(String key, String value){
  36. getProp().set(key, value).store();;
  37. }
  38. /**
  39. * 从配置文件中获取key所对应的值
  40. * @param key 键
  41. * @return value 值
  42. */
  43. public static String g(String key){
  44. return getProp().get(key);
  45. }
  46. /**
  47. * 从配置文件中获取key所对应的值
  48. * @param key 键
  49. * @param defaultValue 键不存在时,返回该默认值
  50. * @return value 值
  51. */
  52. public static String g(String key, String defaultValue){
  53. String value = g(key);
  54. return value == null ? defaultValue : value;
  55. }
  56. /**
  57. * 获取指定key对应的值,转换为int型
  58. */
  59. public static Integer gi(String key) {
  60. return gi(key, null);
  61. }
  62. /**
  63. * 获取指定key对应的值,转换为int型
  64. */
  65. public static Integer gi(String key, Integer defaultValue) {
  66. String value = g(key);
  67. return (value != null) ? Integer.parseInt(value) : defaultValue;
  68. }
  69. /**
  70. * 获取指定key对应的值,转换为long型
  71. */
  72. public static Long gl(String key) {
  73. return gl(key, null);
  74. }
  75. /**
  76. * 获取指定key对应的值,转换为long型
  77. */
  78. public static Long gl(String key, Long defaultValue) {
  79. String value = g(key);
  80. return (value != null) ? Long.parseLong(value) : defaultValue;
  81. }
  82. /**
  83. * 获取指定key对应的值,转换为boolean型
  84. */
  85. public static Boolean gb(String key) {
  86. return gb(key, null);
  87. }
  88. /**
  89. * 获取指定key对应的值,转换为boolean型
  90. */
  91. public static Boolean gb(String key, Boolean defaultValue) {
  92. String value = g(key);
  93. return (value != null) ? Boolean.parseBoolean(value) : defaultValue;
  94. }
  95. public static boolean c(String key) {
  96. return params.containsKey(key);
  97. }
  98. public static interface ConfigLine{
  99. }
  100. public static class ConfigBlank implements ConfigLine{
  101. }
  102. public static class ConfigComment implements ConfigLine{
  103. private String comment;
  104. public ConfigComment(String comment) {
  105. this.comment = comment;
  106. }
  107. public String get(){
  108. return comment;
  109. }
  110. }
  111. public static class ConfigParam implements ConfigLine{
  112. private String key;
  113. private String value;
  114. public ConfigParam(String key, String value) {
  115. this.key = key;
  116. this.value = value;
  117. }
  118. public String getKey(){
  119. return this.key;
  120. }
  121. public ConfigLine set(String value){
  122. this.value = value;
  123. return this;
  124. }
  125. public String get(){
  126. return this.value;
  127. }
  128. public int getInt(){
  129. return Integer.valueOf(this.value);
  130. }
  131. public long getLong(){
  132. return Long.valueOf(this.value);
  133. }
  134. public boolean isTrue(){
  135. return Boolean.valueOf(this.value);
  136. }
  137. public boolean isFalse(){
  138. return !isTrue();
  139. }
  140. }
  141. public static class ConfigBuffer{
  142. private String fileName = null;
  143. private Map<String,ConfigLine> lines = null;
  144. public ConfigBuffer set(String key, String value){
  145. if(lines.containsKey(key)){
  146. ((ConfigParam)lines.get(key)).set(value);
  147. }
  148. return this;
  149. }
  150. public String get(String key){
  151. if(containsKey(key)){
  152. return ((ConfigParam)lines.get(key)).get();
  153. }
  154. return null;
  155. }
  156. public boolean containsKey(String key){
  157. return lines.containsKey(key);
  158. }
  159. public static ConfigBuffer load(String fileName) throws Exception {
  160. InputStream inputStream = C.class.getClassLoader().getResourceAsStream(fileName);
  161. if(inputStream!=null){
  162. ConfigBuffer buffer = new ConfigBuffer();
  163. buffer.fileName = fileName;
  164. buffer.lines = new LinkedHashMap<String,ConfigLine>();
  165. BufferedReader reader = null;
  166. try {
  167. reader = new BufferedReader(new InputStreamReader(inputStream));
  168. int lineNum = 0;
  169. while (true) {
  170. String lineStr = reader.readLine();
  171. if(lineStr==null)break;
  172. if(lineStr.trim().equals("")){
  173. buffer.lines.put(S.f("%02d", ++lineNum), new ConfigBlank());
  174. }else if(lineStr.startsWith("#")){
  175. buffer.lines.put(S.f("%02d", ++lineNum), new ConfigComment(lineStr.substring(1)));
  176. }else{
  177. String key = lineStr.split("=")[0].trim();
  178. String value = lineStr.split("=")[1].trim();
  179. buffer.lines.put(key, new ConfigParam(key,value));
  180. }
  181. }
  182. return buffer;
  183. }catch (Exception e){
  184. throw new RuntimeException("加载配置文件失败", e);
  185. }finally{
  186. if(reader!=null){
  187. try {
  188. reader.close();
  189. } catch (IOException e) {
  190. logger.error("",e);
  191. }
  192. }
  193. }
  194. }else{
  195. throw new IllegalArgumentException(S.f("配置文件[%s]不存在",filePath));
  196. }
  197. }
  198. public void store(){
  199. String filePath = C.class.getClassLoader().getResource(fileName).getFile();
  200. File file = new File(filePath);
  201. if(file.exists()){
  202. file.delete();
  203. }
  204. FileWriter writer = null;
  205. try {
  206. StringBuilder fileContent = new StringBuilder();
  207. for(ConfigLine line : lines.values()){
  208. if(line instanceof ConfigBlank){
  209. fileContent.append("\n");
  210. }else if(line instanceof ConfigComment){
  211. fileContent.append("#");
  212. fileContent.append(((ConfigComment)line).get());
  213. fileContent.append("\n");
  214. }else if(line instanceof ConfigParam){
  215. fileContent.append(((ConfigParam)line).getKey());
  216. fileContent.append("=");
  217. fileContent.append(((ConfigParam)line).get());
  218. fileContent.append("\n");
  219. }
  220. }
  221. writer = new FileWriter(file);
  222. writer.write(fileContent.toString());
  223. writer.flush();
  224. }catch (Exception e){
  225. throw new RuntimeException("保存配置文件失败", e);
  226. }finally{
  227. if(writer!=null){
  228. try {
  229. writer.close();
  230. } catch (IOException e) {
  231. logger.error("",e);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }