使用properties对文件的操作
/**
* 对属性文件(xx.properties)的操作
* 注:属性文件一定要放在当前工程的根目录下, 也就是放在与src目录在同一个目录下
*/
public class OperatePropertiesFile {
public OperatePropertiesFile() {
}
/**
* 采用Properties类取得属性文件对应值
*
* @parampropertiesFileName properties文件名,如a.properties
* @parampropertyName属性名
* @return根据属性名得到的属性值,如没有返回""
*/
private String getValueByPropertyName(String propertiesFileName, String propertyName) {
// 获取src下的文件路径
propertiesFileName = this.getClass().getResource(propertiesFileName).getPath();
//System.out.println(propertiesFileName);
String s = "";
Properties p = new Properties();// 加载属性文件读取类
FileInputStream in;
try {
// propertiesFileName如test.properties
in = new FileInputStream(propertiesFileName);// 以流的形式读入属性文件
p.load(in);// 属性文件将该流加入的可被读取的属性中
in.close();// 读完了关闭
s = p.getProperty(propertyName);// 取得对应的属性值
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
/**
* 采用ResourceBundel类取得属性文件对应值,这个只能够读取,不可以更改及写新的属性
*
* @parampropertiesFileNameWithoutPostfixproperties文件名,不带后缀
* @parampropertyName属性名
* @return根据属性名得到的属性值,如没有返回""
*/
// name后有下划线
// pfwp为propertiesFileNameWithoutPostfix
private String getValueByPropertyName_(String pfwp, String propertyName) {
String s = "";
// 如属性文件是test.properties,那此时pfwp的值就是test
ResourceBundle bundel = ResourceBundle.getBundle(pfwp);
s = bundel.getString(propertyName);
return s;
}
/**
* 更改属性文件的值,如果对应的属性不存在,则自动增加该属性
*
* @parampropertiesFileNameproperties文件名,如a.properties
* @parampropertyName属性名
* @parampropertyValue将属性名更改成该属性值
* @return是否操作成功
*/
private boolean changeValueByPropertyName(String propertiesFileName,
String propertyName, String propertyValue) {
// 获取src下的文件路径
propertiesFileName = this.getClass().getResource(propertiesFileName).getPath();
//System.out.println(propertiesFileName);
boolean writeOK = true;
Properties p = new Properties();
FileInputStream in;
try {
in = new FileInputStream(propertiesFileName);
p.load(in);//
in.close();
p.setProperty(propertyName, propertyValue);// 设置属性值,如不属性不存在新建
// p.setProperty("testProperty","testPropertyValue");
FileOutputStream out = new FileOutputStream(propertiesFileName);// 输出流
p.store(out, "Just Test");// 设置属性头,如不想设置,请把后面一个用""替换掉
out.flush();// 清空缓存,写入磁盘
out.close();// 关闭输出流
} catch (Exception e) {
e.printStackTrace();
}
return writeOK;
}
public static void main(String[] args) {
OperatePropertiesFile opf = new OperatePropertiesFile();
// 文件名必须是"/"+src下的文件名字
opf.changeValueByPropertyName("/db.properties", "TestKey", "TestValue");
// 文件名必须是"/"+src下的文件名字
System.out.println(opf.getValueByPropertyName("/db.properties", "TestKey"));
// 文件名必须是src下的文件名字(没有后缀,没"/")
System.out.println(opf.getValueByPropertyName_("db", "TestKey"));
}
}
属性文件的添加结果;
#Just Test
#Thu Jun 01 09:59:39 CST 2017
TestKey=TestValue
以下来源于http://lavasoft.blog.51cto.com/62575/184605/
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下面。)
myres.properties
aaa=good
bbb=thanks
myres_en_US.properties
aaa=good
bbb=thanks
myres_zh_CN.properties
aaa=\u597d
bbb=\u591a\u8c22
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"));
}
}
运行结果:
好
好
good
Process finished with exit code 0
如果使用默认的Locale,那么在英文操作系统上,会选择myres_en_US.properties或myres.properties资源文件。
三、认识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 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。
语言参数是一个有效的 ISO 语言代码。这些代码是由 ISO-639 定义的小写两字母代码。在许多网站上都可以找到这些代码的完整列表,如:
http://www.loc.gov/standards/iso639-2/englangn.html。
国家参数是一个有效的 ISO 国家代码。这些代码是由 ISO-3166 定义的大写两字母代码。在许多网站上都可以找到这些代码的完整列表,如:
http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html。
四、中文资源文件的转码 native2ascii