一.介绍:
项目中经常把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码.
这里讲两种方式读取properties文件的方法.一个是用HashTable下的Properties类一个是用国际化的ResourceBundle类.
二.第一种:Properties类读取properties配置文件
下面的代码是在一个web工程中运行的,具体解释看代码中的注释:
1 import java.io.InputStream; 2 import java.util.Properties; 3 4 import com.chinadigitalvideo.filter.PropertiesUtil; 5 6 public class PropertiesAction { 7 //把properties中用到的常量又和全局相关声明为static和final类型的. 8 public static final String CLASSDRIVER ; 9 public static final String URL ; 10 public static final String NAME ; 11 public static final String PASSWORD ; 12 13 public static final String RETRYTIMES ; 14 public static final String LOCKDEADLINE ; 15 /* 16 用到Javase中继承于HashTable的Properties类,来读取properties配置文件获得键值对信息. 17 如果是读取一个普通的txt文本文件使用File类来读取。 18 properties文件中都是一些静态的常量,所以把这块读取的代码写在静态代码块中 不是写在普通的方法 19 */ 20 static{ 21 Properties props = new Properties(); 22 /*
方式一: 23 加载读取该配置文件需要使用Properties的load方法,当然load()方法中是读取文件的流 24 props.load(new FileInputStream("src/db.properties")); 25 但是此项目是个web项目,打包发布到服务器上,war包中压根没有src目录,所以不能用用这中路径来读取文件. 26 应该用类的加载器来读取这个文件.怎么通过类加载器获得这个db.properties文件. 27 */ 28 ClassLoader classLoader = PropertiesAction.class.getClassLoader(); 29 InputStream is = classLoader.getResourceAsStream("db.properties"); 30 try { 31 props.load(is); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 CLASSDRIVER = props.getProperty("driverClass"); 36 URL = props.getProperty("url"); 37 NAME = props.getProperty("name"); 38 PASSWORD = props.getProperty("password");
39 /* 40 方式二: 需要PropertiesAction.java 文件和 要读取的login.properties配置文件在一个目录下 */ 43 InputStream inputStream = PropertiesAction.class.getResourceAsStream("login.properties"); 44 try { 45 props.load(inputStream); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 RETRYTIMES = props.getProperty("retrytimes"); 50 LOCKDEADLINE = props.getProperty("lockdeadline"); 51 } 52 }
为说明ClassLoader作用的根目录附录相关截图:
图1:Debug代码,官产ClassLoader对象的信息 和 项目的源码编译路径做比较
图2:打成war包之后的目录如下,WebContent是这个web项目的根目录,打成war包后,所有的代码资源都在此目录下, 源码编译后的class文件在上图设置的路径/WEB-INF/classes下
三.ResourceBundle类读取Properties方法(此处也是放在web项目中用的)
1.示例代码
import java.util.ResourceBundle; public class ResourceBundleAction { //用到的常量又和全局相关声明成static 和final类型的. public static final String CLASSDRIVER ; public static final String URL ; public static final String NAME ; public static final String PASSWORD ; static{ /* javase中的提供的一个国际化的类 ResourceBundle类.使用这个类可以轻松地本地化或翻译成不同的语言 其中getBundle(String baseName)方法的作用是 baseName - 资源包的基本名称,是一个完全限定类名 具有给定基本名称和默认语言环境的资源包 */ CLASSDRIVER = ResourceBundle.getBundle("db").getString("driverClass"); URL = ResourceBundle.getBundle("db").getString("url"); NAME = ResourceBundle.getBundle("db").getString("name"); PASSWORD = ResourceBundle.getBundle("db").getString("password"); } }
2.java.util.ResourceBundle使用详解
myres_zh_CN.properties
TestResourceBundle.java
1 import java.util.Locale; 2 import java.util.ResourceBundle; 3 4 public class TestResourceBundle { 5 public static void main(String[] args) { 6 Locale locale1 = new Locale("zh", "CN"); 7 ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1); 8 System.out.println(resb1.getString("aaa")); 9 10 ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault()); 11 System.out.println(resb1.getString("aaa")); 12 13 Locale locale3 = new Locale("en", "US"); 14 ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3); 15 System.out.println(resb3.getString("aaa")); 16 } 17 }
在src根目录下有:
myres.properties
aaa=good
bbb=thanks
myres_en_US.properties
aaa=good
bbb=thanks
myres_zh_CN.properties
aaa=\u597d
bbb=\u591a\u8c22
运行结果:
好
好
good
3.认识Locale
Locale(String language, String country)
Locale(String language, String country, String variant)
1 package cn.itcast.resoucebundle; 2 3 import java.text.MessageFormat; 4 import java.util.MissingResourceException; 5 import java.util.ResourceBundle; 6 7 public class I18nMessages { 8 public static final String KEY_NOT_FOUND_PREFIX = "!!!"; //$NON-NLS-1$ 9 10 public static final String KEY_NOT_FOUND_SUFFIX = "!!!"; //$NON-NLS-1$ 11 12 private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$ 13 private static final String BUNDLE_NAME2 = "cn.itcast.interfaceAbstract.messages"; //$NON-NLS-1$ 14 15 private static final String PLUGIN_ID = "cn.itcast.resourcebundle"; //$NON-NLS-1$ 16 17 private static ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME2); 18 19 public static String getString(String key, String pluginId, ResourceBundle resourceBundle) { 20 if (resourceBundle == null) { 21 return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX; 22 } 23 try { 24 return resourceBundle.getString(key); 25 } catch (MissingResourceException e) { 26 return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX; 27 } 28 } 29 30 public static String getString(String key, String pluginId, ResourceBundle resourceBundle, Object... args){ 31 return MessageFormat.format(getString(key, pluginId, resourceBundle),args); 32 } 33 34 public static String getString(String key, Object... args){ 35 return getString(key, PLUGIN_ID, resourceBundle, args); 36 } 37 public static void main(String[] args) { 38 String test = "kxh"; 39 String test2 = "Yes,I am"; 40 System.out.println(I18nMessages.getString("name",test,test2));//这个方法设置的可以跟多个参数. 41 } 42 }
输出:
ResourceBundle.getBundle(BUNDLE_NAME2);的时候
Are you kxh?
Yes,I am
This is the second one
ResourceBundle.getBundle(BUNDLE_NAME);的时候
Are you kxh?
Yes,I am
This is the first one
对应的messages.properties中的参数设置
上面用到了MessageFormat.format()方法
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/4117251.html,如需转载请自行联系原作者