一.介绍:

项目中经常把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码.

这里讲两种方式读取properties文件的方法.一个是用HashTable下的Properties类一个是用国际化的ResourceBundle类.

二.第一种:Properties类读取properties配置文件

下面的代码是在一个web工程中运行的,具体解释看代码中的注释:

 import java.io.InputStream;
import java.util.Properties; import com.chinadigitalvideo.filter.PropertiesUtil; public class PropertiesAction {
//把properties中用到的常量又和全局相关声明为static和final类型的.
public static final String CLASSDRIVER ;
public static final String URL ;
public static final String NAME ;
public static final String PASSWORD ; public static final String RETRYTIMES ;
public static final String LOCKDEADLINE ;
/*
用到Javase中继承于HashTable的Properties类,来读取properties配置文件获得键值对信息.
如果是读取一个普通的txt文本文件使用File类来读取。
properties文件中都是一些静态的常量,所以把这块读取的代码写在静态代码块中 不是写在普通的方法
*/
static{
Properties props = new Properties();
/*
方式一:
加载读取该配置文件需要使用Properties的load方法,当然load()方法中是读取文件的流
props.load(new FileInputStream("src/db.properties"));
但是此项目是个web项目,打包发布到服务器上,war包中压根没有src目录,所以不能用用这中路径来读取文件.
应该用类的加载器来读取这个文件.怎么通过类加载器获得这个db.properties文件.
*/
ClassLoader classLoader = PropertiesAction.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("db.properties");
try {
props.load(is);
} catch (Exception e) {
e.printStackTrace();
}
CLASSDRIVER = props.getProperty("driverClass");
URL = props.getProperty("url");
NAME = props.getProperty("name");
PASSWORD = props.getProperty("password");
/*
方式二: 需要PropertiesAction.java 文件和 要读取的login.properties配置文件在一个目录下 */
InputStream inputStream = PropertiesAction.class.getResourceAsStream("login.properties");
try {
props.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
RETRYTIMES = props.getProperty("retrytimes");
LOCKDEADLINE = props.getProperty("lockdeadline");
}
}

为说明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使用详解

说的简单点,这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties,
如果是默认的,直接写为:自定义名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
 
当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties
没有提供语言和地区的资源文件是系统默认的资源文件。
资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.
定义三个资源文件,放到src的根目录下面(必须这样,或者你放到自己配置的calsspath下面)。

TestResourceBundle.java

 import java.util.Locale;
import java.util.ResourceBundle; public class TestResourceBundle {
public static void main(String[] args) {
Locale locale1 = new Locale("zh", "CN");
ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);
System.out.println(resb1.getString("aaa")); ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());
System.out.println(resb1.getString("aaa")); Locale locale3 = new Locale("en", "US");
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
}
}

在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 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的 操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。
使用此类中的构造方法来创建 Locale:
 Locale(String language)
 Locale(String language, String country)
 Locale(String language, String country, String variant)
 
创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。 
 
 package cn.itcast.resoucebundle;

 import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle; public class I18nMessages {
public static final String KEY_NOT_FOUND_PREFIX = "!!!"; //$NON-NLS-1$ public static final String KEY_NOT_FOUND_SUFFIX = "!!!"; //$NON-NLS-1$ private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$
private static final String BUNDLE_NAME2 = "cn.itcast.interfaceAbstract.messages"; //$NON-NLS-1$ private static final String PLUGIN_ID = "cn.itcast.resourcebundle"; //$NON-NLS-1$ private static ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME2); public static String getString(String key, String pluginId, ResourceBundle resourceBundle) {
if (resourceBundle == null) {
return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX;
}
try {
return resourceBundle.getString(key);
} catch (MissingResourceException e) {
return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX;
}
} public static String getString(String key, String pluginId, ResourceBundle resourceBundle, Object... args){
return MessageFormat.format(getString(key, pluginId, resourceBundle),args);
} public static String getString(String key, Object... args){
return getString(key, PLUGIN_ID, resourceBundle, args);
}
public static void main(String[] args) {
String test = "kxh";
String test2 = "Yes,I am";
System.out.println(I18nMessages.getString("name",test,test2));//这个方法设置的可以跟多个参数.
}
}

输出:

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()方法

使用Properties类和ResourceBundle类读取properties文件的更多相关文章

  1. Maven ResourceBundle.getBundle读取Properties异常MissingResourceException: Can't find bundlei解决方法

    参考:https://blog.csdn.net/thousa_ho/article/details/72817616 问题描述 ResourceBundle读取properties配置文件提示 Mi ...

  2. ResourceBundle (读取properties文件及中文乱码解决方法)

    原文:http://blog.csdn.net/joecheungdishuiya/article/details/6304993 public class test { static Resourc ...

  3. Java使用ResourceBundle类读取properties文件中文乱码的解决方案

    Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...

  4. 读取.properties的内容1

    属性文件方便于项目进行更改,在项目开发中用的也是非常的普遍,在这里就把属性文件的读取通过代码进行一个小结: package com.oyy.test; import java.io.BufferedI ...

  5. Java 读取properties 配置文件的几种方式

    基于ClassLoder读取配置文件 Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入 ...

  6. Java 读取 .properties 文件的几种方式

    Java 读取 .properties 配置文件的几种方式   Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...

  7. Java读取Properties配置文件

    1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集.不过Properties的键和值都是字符串 ...

  8. 读取Properties文件以及中文乱码问题

    在java类中常见的读取Properties文件方式,是使用Properties.load(inputStream);的方式但是常常出现中文乱码问题,这就很尴尬了 public synchronize ...

  9. spring利用注解方式实现Java读取properties属性值

    1. 建立properties文件:我在resource下面建立一个config文件夹,config文件夹里面有mytest.properties文件,文件内容如下: sam.username=sam ...

随机推荐

  1. 2018.07.25 bzoj3878: [Ahoi2014&Jsoi2014]奇怪的计算器(线段树)

    传送门 线段树综合. 让我想起一道叫做siano" role="presentation" style="position: relative;"&g ...

  2. 'org.springframework.web.filter.CharacterEncodingFilter' is not assignable to 'javax.servlet.Filter,This inspection lets you spot the following problems that might occur in descriptors that are used t

    1.jar包导入错误,因为maven下载的原因: 删除这样的包,然后让maven重新下载,问题就会解决

  3. 前端程序员经常忽视的一个 JavaScript 面试题

    题目 function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function () { ...

  4. Exception in thread Thread-3:第三个线程意外

    Status:Executing -train.py 02/09/2019 09:33:38 INFO Log level set to: INFO Using TensorFlow backend. ...

  5. (网络流)ACM Computer Factory --POJ --3436

    链接: http://poj.org/problem?id=3436 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#probl ...

  6. OpenGL中的旋转是可以叠加的?

    OpenGL中的旋转是可以叠加的? 1. opengl中的旋转 如:glrogtate(45.0f, 0, 0, 1),是将当前坐标系顺时针旋转45度,然后绘制, 程序如下: ; float line ...

  7. knowledge_map 修改笔记

    20150110 knowledge_map修改的地方: 1.dialog的调整.把结构图中的dialog(另存,新建知识点)调整到全局中(我的列表,knowledge_map_js_showKnow ...

  8. asp.net mvc 在JS中跳转到其它controller/action

    平时在ASP.NET 中经常这样写, $('#loginOut').click(function() {           $.messager.confirm('系统提示', '您确定要退出本次登 ...

  9. PLSQL(PL/SQL)集成Team Foundation Server (TFS),实现数据库代码的版本管理

    PL/SQL是面向Oralcle数据库的集成开发环境,是众多Oracle数据库开发人员的主要工具.由于PL/SQL(百度百科)不仅是一种SQL语言,更是一种过程编程语言,在项目实施过程中,会积累大量除 ...

  10. C#treeView控件单击事件选中节点滞后问题解决方法

    问题描述:在treeView的Click事件中,选中的节点SelectedNode并不是您刚才点击的节点,总是上一次选中的节点,节点选中滞后的问题. 解决方案:在treeView的MouseDown事 ...