J2SE API读取Properties文件的六种方法

java 代码
  1. 1。使用java.util.Properties类的load()方法    
  2. 示例:    
  3. InputStream in = new BufferedInputStream(new FileInputStream(name));    
  4. Properties p = new Properties();    
  5. p.load(in);    
  6.   
  7. 2。使用java.util.ResourceBundle类的getBundle()方法    
  8. 示例:   
  9. ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());    
  10.   
  11. 3。使用java.util.PropertyResourceBundle类的构造函数    
  12. 示例:    
  13. InputStream in = new BufferedInputStream(new FileInputStream(name));    
  14. ResourceBundle rb = new PropertyResourceBundle(in);    
  15.   
  16. 4。使用class变量的getResourceAsStream()方法    
  17. 示例:    
  18. InputStream in = JProperties.class.getResourceAsStream(name);    
  19. Properties p = new Properties();    
  20. p.load(in);    
  21.   
  22. 5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法    
  23. 示例:    
  24. InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);    
  25. Properties p = new Properties();    
  26. p.load(in);    
  27.   
  28. 6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法    
  29. 示例:    
  30. InputStream in = ClassLoader.getSystemResourceAsStream(name);    
  31. Properties p = new Properties();    
  32. p.load(in);    
  33.   
  34. 补充    
  35.   
  36. Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法    
  37. 示例:   
  38. InputStream in = context.getResourceAsStream(path);    
  39. Properties p = new Properties();    
  40. p.load(in);    
  41.   
  42. 完整的示例,可以参考附件文件    
  43. 如何上传文件,谁知道请告诉以下。 只好把source都贴上来了    
  44. JProperties.java文件    
  45.   
  46. /**   
  47. ** This program is free software.   
  48. **   
  49. ** You may redistribute it and/or modify it under the terms of the GNU   
  50. ** General Public License as published by the Free Software Foundation.   
  51. ** Version 2 of the license should be included with this distribution in   
  52. ** the file LICENSE, as well as License.html. If the license is not   
  53. ** included with this distribution, you may find a copy at the FSF web   
  54. ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the   
  55. ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.   
  56. **   
  57. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,   
  58. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR   
  59. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY   
  60. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR   
  61. ** REDISTRIBUTION OF THIS SOFTWARE.   
  62. **/    
  63.   
  64. package com.kindani;    
  65.   
  66. //import javax.servlet.ServletContext;    
  67. import java.util.*;    
  68. import java.io.InputStream;    
  69. import java.io.IOException;    
  70. import java.io.BufferedInputStream;    
  71. import java.io.FileInputStream;    
  72.   
  73. /**   
  74. * 使用J2SE API?取Properties文件的六?方法   
  75. * User: SYNFORM   
  76. * Date: 2005/07/12   
  77. * Time: 18:40:55   
  78. * To change this template use File | Settings | File Templates.   
  79. */    
  80. public class JProperties {    
  81.   
  82. public final static int BY_PROPERTIES = 1;    
  83. public final static int BY_RESOURCEBUNDLE = 2;    
  84. public final static int BY_PROPERTYRESOURCEBUNDLE = 3;    
  85. public final static int BY_CLASS = 4;    
  86. public final static int BY_CLASSLOADER = 5;    
  87. public final static int BY_SYSTEM_CLASSLOADER = 6;    
  88.   
  89. public final static Properties loadProperties(final String name, final int type) throws IOException {    
  90. Properties p = new Properties();    
  91. InputStream in = null;    
  92. if (type == BY_PROPERTIES) {    
  93. in = new BufferedInputStream(new FileInputStream(name));    
  94. assert (in != null);    
  95. p.load(in);    
  96. else if (type == BY_RESOURCEBUNDLE) {    
  97. ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());    
  98. assert (rb != null);    
  99. p = new ResourceBundleAdapter(rb);    
  100. else if (type == BY_PROPERTYRESOURCEBUNDLE) {    
  101. in = new BufferedInputStream(new FileInputStream(name));    
  102. assert (in != null);    
  103. ResourceBundle rb = new PropertyResourceBundle(in);    
  104. p = new ResourceBundleAdapter(rb);    
  105. else if (type == BY_CLASS) {    
  106. assert (JProperties.class.equals(new JProperties().getClass()));    
  107. in = JProperties.class.getResourceAsStream(name);    
  108. assert (in != null);    
  109. p.load(in);    
  110. // return new JProperties().getClass().getResourceAsStream(name);    
  111. else if (type == BY_CLASSLOADER) {    
  112. assert (JProperties.class.getClassLoader().equals(new JProperties().getClass().getClassLoader()));    
  113. in = JProperties.class.getClassLoader().getResourceAsStream(name);    
  114. assert (in != null);    
  115. p.load(in);    
  116. // return new JProperties().getClass().getClassLoader().getResourceAsStream(name);    
  117. else if (type == BY_SYSTEM_CLASSLOADER) {    
  118. in = ClassLoader.getSystemResourceAsStream(name);    
  119. assert (in != null);    
  120. p.load(in);    
  121. }    
  122.   
  123. if (in != null) {    
  124. in.close();    
  125. }    
  126. return p;    
  127.   
  128. }    
  129.   
  130. // ---------------------------------------------- servlet used    
  131. /*   
  132. public static Properties loadProperties(ServletContext context, String path) throws IOException {   
  133. assert (context != null);   
  134. InputStream in = context.getResourceAsStream(path);   
  135. assert (in != null);   
  136. Properties p = new Properties();   
  137. p.load(in);   
  138. in.close();   
  139. return p;   
  140.  
  141. */    
  142. // ---------------------------------------------- support class    
  143.   
  144. /**   
  145. * ResourceBundle Adapter class.   
  146. */    
  147. public static class ResourceBundleAdapter extends Properties {    
  148. public ResourceBundleAdapter(ResourceBundle rb) {    
  149. assert (rb instanceof java.util.PropertyResourceBundle);    
  150. this.rb = rb;    
  151. java.util.Enumeration e = rb.getKeys();    
  152. while (e.hasMoreElements()) {    
  153. Object o = e.nextElement();    
  154. this.put(o, rb.getObject((String) o));    
  155. }    
  156. }    
  157.   
  158. private ResourceBundle rb = null;    
  159.   
  160. public ResourceBundle getBundle(String baseName) {    
  161. return ResourceBundle.getBundle(baseName);    
  162. }    
  163.   
  164. public ResourceBundle getBundle(String baseName, Locale locale) {    
  165. return ResourceBundle.getBundle(baseName, locale);    
  166. }    
  167.   
  168. public ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {    
  169. return ResourceBundle.getBundle(baseName, locale, loader);    
  170. }    
  171.   
  172. public Enumeration getKeys() {    
  173. return rb.getKeys();    
  174. }    
  175.   
  176. public Locale getLocale() {    
  177. return rb.getLocale();    
  178. }    
  179.   
  180. public Object getObject(String key) {    
  181. return rb.getObject(key);    
  182. }    
  183.   
  184. public String getString(String key) {    
  185. return rb.getString(key);    
  186. }    
  187.   
  188. public String[] getStringArray(String key) {    
  189. return rb.getStringArray(key);    
  190. }    
  191.   
  192. protected Object handleGetObject(String key) {    
  193. return ((PropertyResourceBundle) rb).handleGetObject(key);    
  194. }    
  195.   
  196. }    
  197.   
  198. }    
  199.   
  200.   
  201. JPropertiesTest.java文件    
  202.   
  203. /**   
  204. ** This program is free software.   
  205. **   
  206. ** You may redistribute it and/or modify it under the terms of the GNU   
  207. ** General Public License as published by the Free Software Foundation.   
  208. ** Version 2 of the license should be included with this distribution in   
  209. ** the file LICENSE, as well as License.html. If the license is not   
  210. ** included with this distribution, you may find a copy at the FSF web   
  211. ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the   
  212. ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.   
  213. **   
  214. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,   
  215. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR   
  216. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY   
  217. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR   
  218. ** REDISTRIBUTION OF THIS SOFTWARE.   
  219. **/    
  220. package com.kindani.test;    
  221.   
  222. import junit.framework.*;    
  223. import com.kindani.JProperties;    
  224.   
  225. //import javax.servlet.ServletContext;    
  226. import java.util.Properties;    
  227.   
  228. public class JPropertiesTest extends TestCase {    
  229. JProperties jProperties;    
  230. String key = "helloworld.title";    
  231. String value = "Hello World!";    
  232.   
  233. public void testLoadProperties() throws Exception {    
  234. String name = null;    
  235. Properties p = new Properties();    
  236.   
  237. name = "C:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties";    
  238. p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES);    
  239. assertEquals(value, p.getProperty(key));    
  240.   
  241. name = "com.kindani.test.LocalStrings";    
  242. p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);    
  243. assertEquals(value, p.getProperty(key));    
  244. assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));    
  245.   
  246. name = "C:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties";    
  247. p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);    
  248. assertEquals(value, p.getProperty(key));    
  249. assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));    
  250.   
  251. name = "\\com\\kindani\\test\\LocalStrings.properties";    
  252. p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER);    
  253. assertEquals(value, p.getProperty(key));    
  254.   
  255. name = "\\com\\kindani\\test\\LocalStrings.properties";    
  256. p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);    
  257. assertEquals(value, p.getProperty(key));    
  258.   
  259. name = "test\\LocalStrings.properties";    
  260. p = JProperties.loadProperties(name, JProperties.BY_CLASS);    
  261. assertEquals(value, p.getProperty(key));    
  262. }    
  263.   
  264. /*   
  265. public void testLoadProperties2() throws Exception {   
  266. ServletContext context = null;   
  267. String path = null;   
  268. Properties p = null;   
  269. path = "/WEB-INF/classes/LocalStrings.properties";   
  270. p = JProperties.loadProperties(context, path);   
  271. assertEquals(value, p.getProperty(key));   
  272.  
  273. */    
  274. }    
  275.   
  276. properties文件与JPropertiesTest.java文件相同的目录下    
  277. LocalStrings.properties文件    
  278. # $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $    
  279.   
  280. # Default localized resources for example servlets    
  281. # This locale is en_US    
  282.   
  283. helloworld.title=Hello World!    
  284.   
  285. requestinfo.title=Request Information Example    
  286. requestinfo.label.method=Method:    
  287. requestinfo.label.requesturi=Request URI:    
  288. requestinfo.label.protocol=Protocol:    
  289. requestinfo.label.pathinfo=Path Info:    
  290. requestinfo.label.remoteaddr=Remote Address:    
  291.   
  292. requestheader.title=Request Header Example    
  293.   
  294. requestparams.title=Request Parameters Example    
  295. requestparams.params-in-req=Parameters in this request:    
  296. requestparams.no-params=No Parameters, Please enter some    
  297. requestparams.firstname=First Name:    
  298. requestparams.lastname=Last Name:    
  299.   
  300. cookies.title=Cookies Example    
  301. cookies.cookies=Your browser is sending the following cookies:    
  302. cookies.no-cookies=Your browser isn't sending any cookies    
  303. cookies.make-cookie=Create a cookie to send to your browser    
  304. cookies.name=Name:    
  305. cookies.value=Value:    
  306. cookies.set=You just sent the following cookie to your browser:    
  307.   
  308. sessions.title=Sessions Example    
  309. sessions.id=Session ID:    
  310. sessions.created=Created:    
  311. sessions.lastaccessed=Last Accessed:    
  312. sessions.data=The following data is in your session:    
  313. sessions.adddata=Add data to your session    
  314. sessions.dataname=Name of Session Attribute:    
  315. sessions.datavalue=Value of Session Attribute:   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JCreator是一种Java集成开发环境(IDE),用于开发和编写J2SEJava 2平台标准版)应用程序。它是基于Windows平台的,为Java开发者提供了一个友好的界面和一系列方便的功能。 首先,JCreator提供了一个简单易用的界面,使得开发者能够轻松地创建、编辑和管理Java项目。它具有类似于其他流行IDE的布局,包括编辑器窗口、项目导航器、控制台等。 其次,JCreator具有许多有用的功能。例如,它支持自动代码补全功能,可以在编写代码时快速显示和选择可能的选项。它还具有代码重构功能,可以帮助开发者重命名、提取方法、移动代码块等,提高代码的可维护性。 此外,JCreator还提供了调试功能,允许开发者在运行时跟踪和调试代码。它具有断点设置、变量监视和运行时错误报告等功能,帮助开发者找到和修复潜在的错误。 最后,JCreator与J2SE完全兼容。它支持最新的Java语法和标准库,开发者可以使用JCreator开发各种类型的应用程序,包括控制台程序、图形界面应用和Web应用。 综上所述,JCreator是一个功能强大、易于使用的IDE,适用于开发和编写J2SE应用程序。它提供了许多有用的功能和工具,可以提高开发效率,并帮助开发者在Java开发过程中更轻松地进行调试和测试。无论是初学者还是经验丰富的开发者,都可以从JCreator中受益,并快速开发出高质量的Java应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值