`
timothy0754
  • 浏览: 40556 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

读取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:  


分享到:
评论

相关推荐

    java 读取properties文件代码

    读取Properties文件是Java开发中的常见操作,特别是在需要根据配置文件动态改变程序行为的时候。下面我们将详细探讨如何在Java中读取Properties文件。 首先,你需要确保你的项目中包含了一个Properties文件,比如`...

    js读取properties文件

    在JavaScript(JS)环境中,读取.properties文件通常用于处理配置数据或者本地化文本。这些文件在Java开发中广泛使用,但JavaScript同样可以借助一些库或技术来读取它们。下面我们将详细探讨如何在JavaScript中实现...

    Python实现读取Properties配置文件的方法

    本篇文章将详细探讨如何通过Python来读取并解析`.properties`配置文件。 首先,了解`.properties`文件的格式。这种文件通常用于存储配置信息,其中键值对以等号`=`分隔,每一行代表一个键值对,注释以`#`或`!`开始...

    读取properties文件返回map

    1. **properties文件结构** `properties`文件的结构非常简单,每行代表一个键值对,键和值之间用等号`=`或冒号`:`分隔。例如: ``` username=admin password=123456 database.url=jdbc:mysql://localhost:3306/...

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

    本文将详细讲解使用J2SE API来读取Properties文件的六种方法。 1. **使用java.util.Properties类的load()方法** 这是最基本的方法,通过`FileInputStream`打开文件,然后使用`Properties`类的`load()`方法加载内容...

    android中读取properties文件

    在Android开发中,读取`properties`文件是一个常见的任务,主要用于存储配置信息或者与Java中的`.properties`文件进行交互。`.properties`文件是一种简单的键值对格式,常用于跨平台的配置存储。以下是对这个主题的...

    Java读取properties文件的三种方式

    在Java编程中,读取properties文件是常见的任务,主要用于配置应用程序的参数或环境变量。properties文件通常以键值对的形式存储数据,便于管理和修改。本文将详细介绍三种在Java中读取properties文件的方法。 1. ...

    Java源码读写Properties文件.rar

    这个压缩包“Java源码读写Properties文件.rar”包含了一份关于如何使用Java来读取和写入Properties文件的源代码示例。下面我们将详细探讨这个主题。 首先,Properties类是Java的标准库类,位于`java.util`包下,它...

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    在处理Properties文件时,可能会遇到几个常见的问题,包括找不到指定路径、读取正常但文件数据未更新的情况。以下是对这些问题的详细解答。 首先,让我们解决“系统找不到指定路径”的问题。在Java中,加载...

    Java读取Properties文件的六种方法

    ### Java读取Properties文件的六种方法 在Java开发中,`Properties` 文件常用于存储配置信息,如数据库连接字符串、应用配置等。正确且高效地读取这些配置文件对于程序运行至关重要。本文将详细介绍六种不同的方法...

    SSM 读取properties文件

    "SSM 读取properties文件"这个话题聚焦于如何在项目中有效地读取和使用这些配置文件。properties文件通常用于存储应用程序的配置参数,如数据库连接信息、服务器端口、邮件服务设置等,使得这些关键信息能够独立于...

    properties文件读写

    properties文件读写操作

    java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题

    总结一下,处理Java中的Properties文件读写时,需要注意文件路径的准确性、文件的读写权限以及缓存问题。通过以上方法,应该能够有效解决描述中提到的问题。对于提供的"新建文本文档.txt",虽然不是Properties文件,...

    读取properties文件工具类

    这个"读取properties文件工具类"是为了简化程序中对`.properties`文件的读取操作而设计的。通过这样的工具类,开发者可以方便地加载和获取配置文件中的属性值,避免重复编写相同的代码。下面我们将详细探讨`...

    怎样读取properties文件内容

    ### 如何使用Java读取properties文件内容 在Java开发中,`properties`文件是一种非常常见的配置文件格式,它主要用于存储程序的各种配置信息。通过这种方式,可以实现程序与配置的分离,便于维护和调整。本文将详细...

    读取properties文件内容

    ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容

    c#操作properties,读写配置文件

    ### C#操作Properties,读写配置文件 在C#编程中,经常需要处理应用程序的配置信息,例如数据库连接字符串、用户界面的语言设置等。这些配置信息通常存储在配置文件中,便于程序运行时动态加载和修改。C#提供了一种...

    读取properties文件

    ### 读取Properties文件:Java中的配置管理利器 在Java编程中,`Properties`类是处理配置文件(通常为`.properties`格式)的关键工具。这种文件格式被广泛应用于存储应用程序的配置信息,如数据库连接字符串、邮件...

    properties文件的读取

    3. **读取properties文件内容** 一旦文件加载成功,可以使用`getProperty()`方法获取特定键的值: ```java String username = prop.getProperty("username"); String password = prop.getProperty("password"); ...

    读取Properties文件的六种方法

    ### 读取Properties文件的六种方法 在Java开发中,`Properties`文件是一种非常常见的配置文件格式,它主要用于存储程序的各种配置信息。通过不同方式读取这些配置信息,可以提高程序的灵活性与可维护性。本文将详细...

Global site tag (gtag.js) - Google Analytics