首页 > 图灵资讯 > 技术篇>正文

通用Propterties工具类,兼容读jar同级目录的文件。

2023-06-15 09:29:04

1.前言。 properties文件的写作方法比较难用,折中写了一个通用类。2.例子。

1. import2. import3. import4. import5. import6. import7. import8. import9. import10. import11. import12.   13. /**14.  * 阅读Properties综合类15.  * 16.  * @author lijn 2014-02-2517.  */18. public class19. /**20.      * 获得某一类的路径21.      * 22.      * @param name23.      * @return24.      */25. public static26. null;  27. if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {  28. "/").toString().replace("file:/", "")  29. "%20", " ");  30. else31. "/").toString().replace("file:", "")  32. "%20", " ");  33.         }  34. return35.     }  36.   37. /**38.      * 阅读所有property39.      * 40.      * @param filename4.      *            properties文件路径422.      * @return Property的所有集合(map形式)43.      */44. @SuppressWarnings("unchecked")  45. public static46. if (null47. return null;  48.         }  49. class) + filename;  50. new51. null;  52. try53. new BufferedInputStream(new54.             props.load(in);  55. new56.             Enumeration en = props.propertyNames();  57. while58.                 String key = (String) en.nextElement();  59.                 String Property = props.getProperty(key);  60.                 map.put(key, Property);  61.             }  62. return63. // 关闭资源64. catch65.             e.printStackTrace();  66. catch67.             e.printStackTrace();  68. finally69. try70.                 in.close();  71. catch72. // TODO Auto-generated catch block73.                 e.printStackTrace();  74.             }  75.         }  76. return null;  77.     }  78.   79. /**80.      * 获得一个property的值81.      * 82.      * @param filename83.      *            文件名84.      * @param key85.      *            propertykey86.      * @return propertyvalue87.      */88. public static89. if (null == filename || null90. return null;  91.         }  92. class) + filename;  93. new94. null;  95. null;  96. try97. new BufferedInputStream(new98.             props.load(in);  99. new FileOutputStream(new100.             props.setProperty(key, value);  101. "update");  102. // 关闭资源103. catch104.             e.printStackTrace();  105. catch106.             e.printStackTrace();  107. finally108. try109.                 out.close();  110.                 in.close();  111. catch112. // TODO Auto-generated catch block113.                 e.printStackTrace();  114.             }  115.         }  116. return null;  117.     }  118.   119. /**120.      * 获得property的值121.      * 122.      * @param filename123.      *            文件名124.      * @param key125.      *            propertykey126.      * @return propertyvalue127.      */128. public static129. if (null == filename || null130. return null;  131.         }  132. class) + filename;  133. new134. null;  135. try136. new BufferedInputStream(new137.             props.load(in);  138. return139. // 关闭资源140. catch141.             e.printStackTrace();  142. catch143.             e.printStackTrace();  144. finally145. try146.                 in.close();  147. catch148. // TODO Auto-generated catch block149.                 e.printStackTrace();  150.             }  151.         }  152. return null;  153.     }  154.   155. /**156.      * config需要使用.properies文件放在以下157.      * 158.      * @param filename159.      * @param key160.      * @param value161.      * @return162.      */163. public static164. if (null == filename || null165. return null;  166.         }  167. class) + filename;  168. new169. null;  170. null;  171. try172. class.getResourceAsStream("config.properties");  173. // System.out.println(PropertiesUtil.class.getClassLoader().getResource("config.properties"));174. new175.             props.load(b2);  176.   177. new178.             props.setProperty(key, value);  179. "update");  180. // 关闭资源181. catch182.             e.printStackTrace();  183. catch184.             e.printStackTrace();  185. finally1866. try187.                 out.close();  188.                 in.close();  189. catch190. // TODO Auto-generated catch block191.                 e.printStackTrace();  192.             }  193.         }  194. return null;  195.     }  196.   197. /**198.      * 1999年读写jar外的properties文件.      * 200.      * @param filename201.      * @param key202.      * @param value203.      */204. public static void205.             String value) {  206. if (null == filename || null207. return;  208.         }  209. "java.class.path");  210. 0, outPathString  211. "\\") + 1);  212.   213.         String filePath = outPathString + filename;  214. new215. null;  216. null;  217. try218. new BufferedInputStream(new219.             props.load(b2);  220.   221. new FileOutputStream(new222.             props.setProperty(key, value);  223. "update");  224. // 关闭资源225. catch226.             setProp(filename,key,value);  227. catch228.             e.printStackTrace();  229. finally23000. try231. if(null!=out){  232.                  out.close();  233.                 }  234. if(null!=b2){  235.                 b2.close();  236.                 }  237. catch238. // TODO Auto-generated catch block239.                 e.printStackTrace();  240.             }  241.         }  242.   243.     }  244. /**245.      * 以行的形式写ja内的文件246.      * 247.      * @param filename248.      * @param key249.      * @param value250.      */251. public static void252. if (null == filename || null253. return;  254.         }  255. class) + filename;  256.       257. null;  258. null;  259. byte[] cont=new byte[4];  260. "";  261. try262. ///先读取源文件的内容,再追加内容263. new BufferedInputStream(new264. int265. while(res!=-1){  266. new267.                 res=b2.read(cont);  268.             }  269. //追加270.             contentResult+=content;  271. new FileOutputStream(new272.             out.write(contentResult.getBytes());  273. // 关闭资源274. catch275. catch276.             e.printStackTrace();  277. finally278. try279. if(null!=out){  280.                  out.close();  281.                 }  282. if(null!=b2){  283.                 b2.close();  284.                 }  285. catch286. // TODO Auto-generated catch block287.                 e.printStackTrace();  288.             }  289.         }  290.   291.   292.     }  293. /**294.      * 以行的形式写jar以外的文件295.      * 296.      * @param filename297.      * @param key298.      * @param value299.      */300. public static void301. if (null == filename || null302. return;  303.         }  304. "java.class.path");  305. 0, outPathString  306. "\\") + 1);  307.   308.         String filePath = outPathString + filename;  309. null;  310. null;  311. byte[] cont=new byte[1024];  312. "";  313. try314. ///先读取源文件的内容,再追加内容315. new BufferedInputStream(new316. int317. while(res!=-1){  318. new319.                 res=b2.read(cont);  320.             }  321. //追加322.             contentResult+=content;  323. new324.             out.write(contentResult.getBytes());  325. // 关闭资源326. catch327.             writeLineInerFile(filename,content);  328. catch329.             e.printStackTrace();  330. finally331. try332. if(null!=out){  333.                  out.close();  334.                 }  335. if(null!=b2){  336.                 b2.close();  337.                 }  338. catch339. // TODO Auto-generated catch block340.                 e.printStackTrace();  341.             }  342.         }  343.   344.     }  345. /**346.      * 阅读jar外properties文件的属性347.      * 348.      * @param filename349.      * @param key350.      * @param value351.      */352. public static353. if (null == filename || null354. return null;  355.         }  356. "java.class.path");  357. 0, outPathString  358. "\\") + 1);  359.   360.         String filePath = outPathString + filename;  361. new362. null;  363. try364. new BufferedInputStream(new365.             props.load(b2);  366. return367. // 关闭资源368. catch369. return370. catch371.             e.printStackTrace();  372. finally373. try374. if(null!=b2){  375.                     b2.close();  376.                 }  377. catch378. // TODO Auto-generated catch block379.                 e.printStackTrace();  380.             }  381.         }  382. return null;  383.   384.     }  385.   386. public static void main(String[] args) throws387. "error.txt","fiowe");  388.   389.     }  390. }

上一篇 事务的属性2
下一篇 [转]Spring加载resource时classpath*:与classpath:的区别

文章素材均来源于网络,如有侵权,请联系管理员删除。