一、简介
<1> java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式。
<2> 在properties文件中,可以用"#"来作注释。
properties文件在Java编程中用到的地方很多,操作很方便。
二、Java的Properties类
属性映射(property map):是一种存储键/值对的数据结构。属性映射经常被用来存放配置信息。
它有三个特性:
键和值斗志字符串键/值对可以很容易地写入文件或从文件读出。用二级表存放默认值
实现属性映射的Java类被称为Properties(Java.util.Properties),此类是Java中比较重要的类,主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。
此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步。
Properties类继承自Hashtable,如下:
构造方法:
Properties() 创建一个无默认值的空属性列表。Properties(Properties defaults) 创建一个带有指定默认值的空属性列表。
它提供了几个主要的方法: 1. getProperty ( String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。 2. load ( InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。 3. setProperty ( String key, String value) :调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。 4. store ( OutputStream out, String comments):以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。 5. clear ():清除所有装载的 键 - 值对。该方法在基类中提供。
因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。
如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。
类似地,如果在“不安全”的 Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。
Properties类提供默认值的两种机制: <1>在试图获得字符串值时制定默认值。(当键值不存在的时候,就会自动时用它)
String title=settings.getProperty("title","Default title");
<2>如果觉得每次调用都指定默认值太麻烦,那么就可以将所有的默认值放在一个二级属性映射中,并在主映射的构造器中提供映射。且用它来构造查询表。
Properties defaultSettings=new properties(); defaultSettings.setProperty("width","300"); defaultSettings.setProperty("height","200"); ... Properties settings=new properties(defaultSettings);
注意:属性映射是没有层次结构的简单表。但是可以简单的使用java中的包命名方式来简单伪装一下层次结构。如果要存储复杂的配置信息,就应该使用Preferences类。
三、Java读取Properties文件的方法
<1>Java虚拟机(JVM)有自己的系统配置文件(system.properties),我们可以通过下面的方式来获取。
//获取JVM的系统属性 import java.util.Properties; public class ReadJVM { public static void main(String[] args) { Properties pps = System.getProperties(); pps.list(System.out); } }
<2>使用J2SE API读取Properties文件的六种方法
1. 使用java.util.Properties类的load()方法示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));Properties p = new Properties();p.load(in);
详细示例一:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; /* * 新建一个配置文件(Test.properties),内容可以录入下面的语句。 * name=JJ * Weight=4444 * Height=3333 * * 注意:配置文件一定要放到项目的根目录。(此处没有异常处理) * */ public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream("Test.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } }
详细示例二:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties; //关于Properties类常用的操作 public class TestProperties { //根据Key读取Value public static String GetValueByKey(String filePath, String key) { Properties pps = new Properties(); try { InputStream in = new BufferedInputStream (new FileInputStream(filePath)); pps.load(in); String value = pps.getProperty(key); System.out.println(key + " = " + value); return value; }catch (IOException e) { e.printStackTrace(); return null; } } //读取Properties的全部信息 public static void GetAllProperties(String filePath) throws IOException { Properties pps = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream(filePath)); pps.load(in); Enumeration en = pps.propertyNames(); //得到配置文件的名字 while(en.hasMoreElements()) { String strKey = (String) en.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } //写入Properties信息 public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException { Properties pps = new Properties(); InputStream in = new FileInputStream(filePath); //从输入流中读取属性列表(键和元素对) pps.load(in); //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 OutputStream out = new FileOutputStream(filePath); pps.setProperty(pKey, pValue); //以适合使用 load 方法加载到 Properties 表中的格式, //将此 Properties 表中的属性列表(键和元素对)写入输出流 pps.store(out, "Update " + pKey + " name"); } public static void main(String [] args) throws IOException{ WriteProperties("Test.properties","long", "212"); String value = GetValueByKey("Test.properties", "name"); System.out.println(value); GetAllProperties("Test.properties"); } }
2.使用class变量的getResourceAsStream()方法 示例:
InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in); 详细示例:
import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; /* * 新建一个配置文件(Test.properties),内容可以录入下面的语句。 * name=JJ * Weight=4444 * Height=3333 * *注意:配置文件一定要放到当前目录下。(目录层次也可以从src下面的文件夹开始但不必包含src,且不必包含反斜杠开头。) * */ public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(getProperties.class.getResourceAsStream("Test.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } }
3.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in); 详细示例:
import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; /* * 新建一个配置文件(Test.properties),内容可以录入下面的语句。 * name=JJ * Weight=4444 * Height=3333 * *注意:配置文件一定要放到src目录下。 * */ public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(getProperties.class.getClassLoader().getResourceAsStream("Test.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } }
4.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); 详细示例:
import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; /* * 新建一个配置文件(Test.properties),内容可以录入下面的语句。 * name=JJ * Weight=4444 * Height=3333 * * 注意:配置文件一定要放到src目录下。 * */ public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(ClassLoader.getSystemResourceAsStream("Test.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } }
5.使用java.util.ResourceBundle类的getBundle()方法 示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
这个类提供软件国际化的捷径。通过此类,可以使您所编写的程序可以:
轻松地本地化或翻译成不同的语言一次处理多个语言环境以后可以轻松地进行修改,支持更多的语言环境
说的简单点,这个类的作用就是读取资源属性文件(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.
详细示例:
import java.util.Locale; import java.util.ResourceBundle; /** * 国际化资源绑定测试 * * @author leizhimin 2009-7-29 21:17:42 */ 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")); } } //如果使用默认的Locale,那么在英文操作系统上,会选择myres_en_US.properties或myres.properties资源文件。
6.使用java.util.PropertyResourceBundle类的构造函数
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
PropertyResourceBundle是ResourceBundle的具体子类,是通过对属性文件的静态字符串管理来语言环境资源。 与其他资源包类型不同,不能为 PropertyResourceBundle 创建子类。相反,要提供含有资源数据的属性文件。ResourceBundle.getBundle 将自动查找合适的属性文件并创建引用该文件的 PropertyResourceBundle 具体用法如下: 创建一个属性文件:conf.properties,内容如下: BODWEBSERVICEIPADDRESS=D:\\work\\LCEclipse\\workspace\\bodportal\\webapps\\bod\\WEB-INF\\classes\\sysconfig\\policyconfig.xml 在程序中,通过PropertyResourceBundle获取: private final static String PROPERTIES_NAME = "conf"; public static String getProperties(String configName){ PropertyResourceBundle prbConfig = (PropertyResourceBundle)PropertyResourceBundle.getBundle(PROPERTIES_NAME); return prbConfig.getString(configName); }
四、认识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 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。
五、中文资源文件的转码 native2ascii
这个工具用法如下:
如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果了。
看明白这个了,就不在为struts等web框架的国际化而感到稀奇了。
六、示例
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties; public class TestMain { // 根据key读取value public static String readValue(String filePath, String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); String value = props.getProperty(key); System.out.println(key + value); return value; } catch (Exception e) { e.printStackTrace(); return null; } } // 读取properties的全部信息 public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key); System.out.println(key + Property); } } catch (Exception e) { e.printStackTrace(); } } // 写入properties信息 public static void writeProperties(String filePath, String parameterName, String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); // 从输入流中读取属性列表(键和元素对) prop.load(fis); // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); // 以适合使用 load 方法加载到 Properties 表中的格式, // 将此 Properties 表中的属性列表(键和元素对)写入输出流 prop.store(fos, "Update '" + parameterName + "' value"); } catch (IOException e) { System.err.println("Visit " + filePath + " for updating " + parameterName + " value error"); } } public static void main(String[] args) { readValue("info.properties", "url"); writeProperties("info.properties", "age", "21"); readProperties("info.properties"); System.out.println("OK"); }
https://blog.csdn.net/fanxiaobin577328725/article/details/52071310