读取Properties文件

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

 
发布时间:2007.07.10 06:25     来源:赛迪网    作者:dxaw

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

1.使用java.util.Properties类的load()方法

示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));

Properties p = new Properties();

p.load(in);

2.使用java.util.ResourceBundle类的getBundle()方法

示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.使用java.util.PropertyResourceBundle类的构造函数

示例: InputStream in = new BufferedInputStream(new FileInputStream(name));

ResourceBundle rb = new PropertyResourceBundle(in);

4.使用class变量的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);

Properties p = new Properties();

p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

示例:InputStream in = context.getResourceAsStream(path);

Properties p = new Properties();

p.load(in);

JProperties.java文件
java 代码
  1. package com.kindani;  
  2.   
  3. //import javax.servlet.ServletContext;  
  4. import java.util.*;  
  5. import java.io.InputStream;  
  6. import java.io.IOException;  
  7. import java.io.BufferedInputStream;  
  8. import java.io.FileInputStream;  
  9.   
  10. /** 
  11.  * 使用J2SE API?取Properties文件的六种方法 
  12.  * User: SYNFORM 
  13.  * Date: 2005/07/12 
  14.  * Time: 18:40:55 
  15.  * To change this template use File | Settings | File Templates. 
  16.  */  
  17. public class JProperties {  
  18.   
  19.     public final static int BY_PROPERTIES = 1;  
  20.     public final static int BY_RESOURCEBUNDLE = 2;  
  21.     public final static int BY_PROPERTYRESOURCEBUNDLE = 3;  
  22.     public final static int BY_CLASS = 4;  
  23.     public final static int BY_CLASSLOADER = 5;  
  24.     public final static int BY_SYSTEM_CLASSLOADER = 6;  
  25.   
  26.     public final static Properties loadProperties(  
  27. final String name, final int type) throws IOException {  
  28.         Properties p = new Properties();  
  29.         InputStream in = null;  
  30.         if (type == BY_PROPERTIES) {  
  31.             in = new BufferedInputStream(new FileInputStream(name));  
  32.             assert (in != null);  
  33.             p.load(in);  
  34.         } else if (type == BY_RESOURCEBUNDLE) {  
  35.             ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());  
  36.             assert (rb != null);  
  37.             p = new ResourceBundleAdapter(rb);  
  38.         } else if (type == BY_PROPERTYRESOURCEBUNDLE) {  
  39.             in = new BufferedInputStream(new FileInputStream(name));  
  40.             assert (in != null);  
  41.             ResourceBundle rb = new PropertyResourceBundle(in);  
  42.             p = new ResourceBundleAdapter(rb);  
  43.         } else if (type == BY_CLASS) {  
  44.             assert (JProperties.class.equals(new JProperties().getClass()));  
  45.             in = JProperties.class.getResourceAsStream(name);  
  46.             assert (in != null);  
  47.             p.load(in);  
  48.             //return new JProperties().getClass().getResourceAsStream(name);  
  49.         } else if (type == BY_CLASSLOADER) {  
  50.             assert (JProperties.class.getClassLoader().equals(  
  51. new JProperties().getClass().getClassLoader()));  
  52.             in = JProperties.class.getClassLoader().getResourceAsStream(name);  
  53.             assert (in != null);  
  54.             p.load(in);  
  55.             //return new JProperties().getClass().  
  56. getClassLoader().getResourceAsStream(name);  
  57.         } else if (type == BY_SYSTEM_CLASSLOADER) {  
  58.             in = ClassLoader.getSystemResourceAsStream(name);  
  59.             assert (in != null);  
  60.             p.load(in);  
  61.         }  
  62.   
  63.         if (in != null) {  
  64.             in.close();  
  65.         }  
  66.         return p;  
  67.   
  68.     }  
  69.   
  70.     // ---------------------------------------------- servlet used  
  71. /* 
  72.     public static Properties loadProperties( 
  73. ServletContext context, String path) throws IOException { 
  74.         assert (context != null); 
  75.         InputStream in = context.getResourceAsStream(path); 
  76.         assert (in != null); 
  77.         Properties p = new Properties(); 
  78.         p.load(in); 
  79.         in.close(); 
  80.         return p; 
  81.     } 
  82. */  
  83.   
  84.     // ---------------------------------------------- support class  
  85.   
  86.     /** 
  87.      * ResourceBundle Adapter class. 
  88.      */  
  89.     public static class ResourceBundleAdapter extends Properties {  
  90.         public ResourceBundleAdapter(ResourceBundle rb) {  
  91.             assert (rb instanceof java.util.PropertyResourceBundle);  
  92.             this.rb = rb;  
  93.             java.util.Enumeration e = rb.getKeys();  
  94.             while (e.hasMoreElements()) {  
  95.                 Object o = e.nextElement();  
  96.                 this.put(o, rb.getObject((String) o));  
  97.             }  
  98.         }  
  99.   
  100.         private ResourceBundle rb = null;  
  101.   
  102.         public ResourceBundle getBundle(String baseName) {  
  103.             return ResourceBundle.getBundle(baseName);  
  104.         }  
  105.   
  106.         public ResourceBundle getBundle(String baseName, Locale locale) {  
  107.             return ResourceBundle.getBundle(baseName, locale);  
  108.         }  
  109.   
  110.         public ResourceBundle getBundle(String baseName,   
  111. Locale locale, ClassLoader loader) {  
  112.             return ResourceBundle.getBundle(baseName, locale, loader);  
  113.         }  
  114.   
  115.         public Enumeration<String> getKeys() {  
  116.             return rb.getKeys();  
  117.         }  
  118.   
  119.         public Locale getLocale() {  
  120.             return rb.getLocale();  
  121.         }  
  122.   
  123.         public Object getObject(String key) {  
  124.             return rb.getObject(key);  
  125.         }  
  126.   
  127.         public String getString(String key) {  
  128.             return rb.getString(key);  
  129.         }  
  130.   
  131.         public String[] getStringArray(String key) {  
  132.             return rb.getStringArray(key);  
  133.         }  
  134.   
  135.         protected Object handleGetObject(String key) {  
  136.             return ((PropertyResourceBundle) rb).handleGetObject(key);  
  137.         }  
  138.   
  139.     }  
  140.   
  141. }  
JPropertiesTest.java文件
java 代码
  1. package com.kindani.test;  
  2.   
  3. import junit.framework.*;  
  4. import com.kindani.JProperties;  
  5.   
  6. //import javax.servlet.ServletContext;  
  7. import java.util.Properties;  
  8.   
  9. public class JPropertiesTest extends TestCase {  
  10.     JProperties jProperties;  
  11.     String key = "helloworld.title";  
  12.     String value = "Hello World!";  
  13.   
  14.     public void testLoadProperties() throws Exception {  
  15.         String name = null;  
  16.         Properties p = new Properties();  
  17.   
  18.         name = "C:\\IDEAP\\Properties4Methods\\src\\com\\ 
  19. kindani\\test\\LocalStrings.properties";  
  20.         p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES);  
  21.         assertEquals(value, p.getProperty(key));  
  22.   
  23.         name = "com.kindani.test.LocalStrings";  
  24.         p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);  
  25.         assertEquals(value, p.getProperty(key));  
  26.         assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));  
  27.   
  28.         name = "C:\\IDEAP\\Properties4Methods\\src\\com\\ 
  29. kindani\\test\\LocalStrings.properties";  
  30.         p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);  
  31.         assertEquals(value, p.getProperty(key));  
  32.         assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));  
  33.   
  34.         name = "\\com\\kindani\\test\\LocalStrings.properties";  
  35.         p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER);  
  36.         assertEquals(value, p.getProperty(key));  
  37.   
  38.         name = "\\com\\kindani\\test\\LocalStrings.properties";  
  39.         p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);  
  40.         assertEquals(value, p.getProperty(key));  
  41.   
  42.         name = "test\\LocalStrings.properties";  
  43.         p = JProperties.loadProperties(name, JProperties.BY_CLASS);  
  44.         assertEquals(value, p.getProperty(key));  
  45.     }  
  46.   
  47. /* 
  48.     public void testLoadProperties2() throws Exception { 
  49.         ServletContext context = null; 
  50.         String path = null; 
  51.         Properties p = null; 
  52.         path = "/WEB-INF/classes/LocalStrings.properties"; 
  53.         p = JProperties.loadProperties(context, path); 
  54.         assertEquals(value, p.getProperty(key)); 
  55.     } 
  56. */  
  57. }  

properties文件与JPropertiesTest.java文件相同的目录下

LocalStrings.properties文件

java 代码
 
  1. # $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $  
  2.   
  3. # Default localized resources for example servlets  
  4. # This locale is en_US  
  5.   
  6. helloworld.title=Hello World!  
  7.   
  8. requestinfo.title=Request Information Example  
  9. requestinfo.label.method=Method:  
  10. requestinfo.label.requesturi=Request URI:  
  11. requestinfo.label.protocol=Protocol:  
  12. requestinfo.label.pathinfo=Path Info:  
  13. requestinfo.label.remoteaddr=Remote Address:  
  14.   
  15. requestheader.title=Request Header Example  
  16.   
  17. requestparams.title=Request Parameters Example  
  18. requestparams.params-in-req=Parameters in this request:  
  19. requestparams.no-params=No Parameters, Please enter some  
  20. requestparams.firstname=First Name:  
  21. requestparams.lastname=Last Name:  
  22.   
  23. cookies.title=Cookies Example  
  24. cookies.cookies=Your browser is sending the following cookies:  
  25. cookies.no-cookies=Your browser isn't sending any cookies  
  26. cookies.make-cookie=Create a cookie to send to your browser  
  27. cookies.name=Name:  
  28. cookies.value=Value:  
  29. cookies.set=You just sent the following cookie to your browser:  
  30.   
  31. sessions.title=Sessions Example  
  32. sessions.id=Session ID:  
  33. sessions.created=Created:  
  34. sessions.lastaccessed=Last Accessed:  
  35. sessions.data=The following data is in your session:  
  36. sessions.adddata=Add data to your session  
  37. sessions.dataname=Name of Session Attribute:  
  38. sessions.datavalue=Value of Session Attribute:  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值