From http://www.blogjava.net/zhujiang001/archive/2005/10/27/16980.html
1. Use the load () method of java. util. Properties class
Example:
InputStream in = lnew BufferedInputStream (new FileInputStream (name ));
Properties p = new Properties ();
P. load (in );
2. Use the getBundle () method of the java. util. ResourceBundle class
Example:
ResourceBundle rb = ResourceBundle. getBundle (name, Locale. getDefault ());
3. Use the constructor of the java. util. PropertyResourceBundle class
Example:
InputStream in = new BufferedInputStream (new FileInputStream (name ));
ResourceBundle rb = new PropertyResourceBundle (in );
4. Use the getResourceAsStream () method of the class variable
Example:
InputStream in = JProperties. class. getResourceAsStream (name );
Properties p = new Properties ();
P. load (in );
5. GetResourceAsStream () method of java. lang. ClassLoader obtained by using class. getClassLoader ()
Example:
InputStream in = JProperties. class. getClassLoader (). getResourceAsStream (name );
Properties p = new Properties ();
P. load (in );
6. Use the getSystemResourceAsStream () Static Method of the java. lang. ClassLoader class
Example:
InputStream in = ClassLoader. getSystemResourceAsStream (name );
Properties p = new Properties ();
P. load (in );
Supplement
You can use the getResourceAsStream () method of javax. Servlet. ServletContext in servlet.
Example:
InputStream in = context. getResourceAsStream (path );
Properties p = new Properties ();
P. load (in );
For a complete example, refer to the attachment file.
JProperties. java File
/**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license shocould be removed with this distribution in
** The file license, as well as license.html. If the license is not
** Removed ded with this distribution, you may find a copy at the FSF Web
** Site at 'www .gnu.org 'or 'www .fsf.org', or you may write to
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** This software is provided as-is without warranty of any kind,
** Not even the implied warranty of merchantability. The author
** Of this software, assumes _ No _ responsibility for any
** Consequence resulting from the use, modification, or
** Redistribution of this software.
**/
Package com. kindani;
// Import javax. servlet. ServletContext;
Import java. util .*;
Import java. io. InputStream;
Import java. io. IOException;
Import java. io. BufferedInputStream;
Import java. io. FileInputStream;
/**
* Use J2SE API interfaces to retrieve the Properties file in six ways
* User: SYNFORM
* Date: 2005/07/12
* Time: 18:40:55
* To change this template use File | Settings | File Templates.
*/
Public class JProperties {
Public final static int BY_PROPERTIES = 1;
Public final static int BY_RESOURCEBUNDLE = 2;
Public final static int BY_PROPERTYRESOURCEBUNDLE = 3;
Public final static int BY_CLASS = 4;
Public final static int BY_CLASSLOADER = 5;
Public final static int BY_SYSTEM_CLASSLOADER = 6;
Public final static Properties loadProperties (final String name, final int type) throws IOException {
Properties p = new Properties ();
InputStream in = null;
If (type = BY_PROPERTIES ){
In = new BufferedInputStream (new FileInputStream (name ));
Assert (in! = Null );
P. load (in );
} Else if (type = BY_RESOURCEBUNDLE ){
ResourceBundle rb = ResourceBundle. getBundle (name, Locale. getDefault ());
Assert (rb! = Null );
P = new ResourceBundleAdapter (rb );
} Else if (type = BY_PROPERTYRESOURCEBUNDLE ){
In = new BufferedInputStream (new FileInputStream (name ));
Assert (in! = Null );
ResourceBundle rb = new PropertyResourceBundle (in );
P = new ResourceBundleAdapter (rb );
} Else if (type = BY_CLASS ){
Assert (JProperties. class. equals (new JProperties (). getClass ()));
In = JProperties. class. getResourceAsStream (name );
Assert (in! = Null );
P. load (in );
// Return new JProperties (). getClass (). getResourceAsStream (name );
} Else if (type = BY_CLASSLOADER ){
Assert (jproperties. Class. getclassloader (). Equals (New jproperties (). getclass (). getclassloader ()));
In = jproperties. Class. getclassloader (). getresourceasstream (name );
Assert (in! = NULL );
P. Load (in );
// Return New jproperties (). getclass (). getclassloader (). getresourceasstream (name );
} Else if (type = by_system_classloader ){
In = classloader. getsystemresourceasstream (name );
Assert (in! = Null );
P. load (in );
}
If (in! = Null ){
In. close ();
}
Return p;
}
// ------------------------------------------------ Servlet used
/*
Public static Properties loadProperties (ServletContext context, String path) throws IOException {
Assert (context! = Null );
Inputstream in = context. getresourceasstream (PATH );
Assert (in! = NULL );
Properties P = new properties ();
P. Load (in );
In. Close ();
Return P;
}
*/
// ------------------------------------------------ Support Class
/**
* Resourcebundle adapter class.
*/
Public static class resourcebundleadapter extends properties {
Public ResourceBundleAdapter (ResourceBundle rb ){
Assert (rb instanceof java. util. PropertyResourceBundle );
This. rb = rb;
Java. util. Enumeration e = rb. getKeys ();
While (e. hasMoreElements ()){
Object o = e. nextElement ();
This. put (o, rb. getObject (String) o ));
}
}
Private ResourceBundle rb = null;
Public ResourceBundle getBundle (String baseName ){
Return ResourceBundle. getBundle (baseName );
}
Public ResourceBundle getBundle (String baseName, Locale locale ){
Return ResourceBundle. getBundle (baseName, locale );
}
Public ResourceBundle getBundle (String baseName, Locale locale, ClassLoader loader ){
Return resourcebundle. getbundle (basename, locale, loader );
}
Public enumeration <string> getkeys (){
Return RB. getkeys ();
}
Public locale getlocale (){
Return RB. getlocale ();
}
Public object GetObject (string key ){
Return RB. GetObject (key );
}
Public String getString (String key ){
Return rb. getString (key );
}
Public String [] getStringArray (String key ){
Return rb. getStringArray (key );
}
Protected Object handleGetObject (String key ){
Return (PropertyResourceBundle) rb). handleGetObject (key );
}
}
}
JPropertiesTest. java File
/**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license shocould be removed with this distribution in
** The file LICENSE, as well as License.html. If the license is not
** Removed ded with this distribution, you may find a copy at the FSF web
** Site at 'www .gnu.org 'or 'www .fsf.org', or you may write to
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** This software is provided as-is without warranty of any kind,
** Not even the implied warranty of merchantability. THE AUTHOR
** Of this software, ASSUMES _ NO _ RESPONSIBILITY FOR ANY
** Consequence resulting from the use, modification, or
** Redistribution of this software.
**/
Package com. kindani. test;
Import JUnit. Framework .*;
Import com. kindani. jproperties;
// Import javax. servlet. servletcontext;
Import java. util. properties;
Public class jpropertiestest extends testcase {
Jproperties;
String key = "helloworld. Title ";
String value = "Hello world! ";
Public void testloadproperties () throws exception {
String name = NULL;
Properties P = new properties ();
Name = "C :\\ ideap \ properties4methods \ SRC \ com \ kindani \ test \ localstrings. properties ";
P = jproperties. loadproperties (name, jproperties. by_properties );
Assertequals (value, P. getproperty (key ));
Name = "com. kindani. Test. localstrings ";
P = jproperties. loadproperties (name, jproperties. by_resourcebundle );
Assertequals (value, P. getproperty (key ));
Assertequals (value, (jproperties. resourcebundleadapter) P). getstring (key ));
Name = "C :\\ ideap \ properties4methods \ SRC \ com \ kindani \ test \ localstrings. properties ";
P = jproperties. loadproperties (name, jproperties. by_propertyresourcebundle );
Assertequals (value, P. getproperty (key ));
Assertequals (value, (jproperties. resourcebundleadapter) P). getstring (key ));
Name = "\ com \ kindani \ test \ localstrings. properties ";
P = jproperties. loadproperties (name, jproperties. by_system_classloader );
Assertequals (value, P. getproperty (key ));
Name = "\ com \ kindani \ test \ localstrings. properties ";
P = jproperties. loadproperties (name, jproperties. by_classloader );
AssertEquals (value, p. getProperty (key ));
Name = "test \ LocalStrings. properties ";
P = JProperties. loadProperties (name, JProperties. BY_CLASS );
AssertEquals (value, p. getProperty (key ));
}
/*
Public void testLoadProperties2 () throws Exception {
ServletContext context = null;
String path = null;
Properties p = null;
Path = "/WEB-INF/classes/LocalStrings. properties ";
P = JProperties. loadProperties (context, path );
AssertEquals (value, p. getProperty (key ));
}
*/
}
The properties file is in the same directory as the JPropertiesTest. java file.
LocalStrings. properties File
# $ Id: LocalStrings. properties, v 1.1 2000/08/17 00:57:52 horwat Exp $
# Default localized resources for example servlets
# This locale is en_US
Helloworld. title = Hello World!
Requestinfo. title = Request Information Example
Requestinfo. label. method = Method:
Requestinfo. label. requesturi = Request URI:
Requestinfo. label. protocol = Protocol:
Requestinfo. label. pathinfo = Path Info:
Requestinfo. label. remoteaddr = Remote Address:
Requestheader. title = Request Header Example
Requestparams. title = Request Parameters Example
Requestparams. params-in-req = Parameters in this request:
Requestparams. no-params = No Parameters, Please enter some
Requestparams. firstname = First Name:
Requestparams. lastname = Last Name:
Cookies. title = Cookies Example
Cookies. cookies = Your browser is sending the following cookies:
Cookies. no-cookies = Your browser isn' t sending any cookies
Cookies. make-cookie = Create a cookie to send to your browser
Cookies. name = Name:
Cookies. value = Value:
Cookies. set = You just sent the following cookie to your browser:
Sessions. title = Sessions Example
Sessions. id = Session ID:
Sessions. created = Created:
Sessions. lastaccessed = Last Accessed:
Sessions. Data = the following data is in your session:
Sessions. adddata = add data to your session
Sessions. dataname = Name of session attribute:
Sessions. datavalue = value of session attribute: