一、简介

Java中的properties文件是一种纯文本格式的配置文件,主要用于表达配置信息,文件类型为 *.properties,文件中内容的格式是 "键=值" 的格式。在properties文件中,可以用井号"#"来作注释

properties文件在Java编程中用到的地方很多,操作很方便。

二、Java的Properties类

属性映射(property map):是一种存储键/值对的数据结构。属性映射经常被用来存放配置信息。

它有三个特性:

  1. 键和值都是字符串
  2. 键/值对可以很容易地写入文件或从文件读出。
  3. 用二级表存放默认值

实现属性映射的Java类被称为Properties(Java.util.Properties),此类是Java中比较重要的类,主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。

此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步。

Properties类继承自Hashtable,如下:

构造方法:

  • Properties() 创建一个无默认值的空属性列表
  • Properties(Properties defaults) 创建一个带有指定默认值的空属性列表

它提供了几个主要的方法:

  • getProperty ( String key):用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
  • load ( InputStream inStream):从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
  • setProperty ( String key, String value) :调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
  • store ( OutputStream out, String comments):以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
  • 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文件的方法

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);}
}

使用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读取Valuepublic 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{<span style="font-family: Arial, Helvetica, sans-serif;">    </span>
<span style="white-space:pre"> </span>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读取valuepublic 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");}
}

参考资料:

  • 《java核心技术 卷I》
  • 《JDK_API_1.6_中文版》
  • Java中Properties类的操作
  • Java读取Properties文件的六种方法
  • JAVA操作properties文件
  • java.util.ResourceBundle使用详解
  • PropertyResourceBundle的使用

JAVA 操作 properties 配置文件相关推荐

  1. JAVA操作properties配置文件

    一.简介 <1> java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值&quo ...

  2. java操作properties文件简单学习

    java操作properties文件的工具类简单封装: 注意:由于本地和环境和linux服务的路径有区别,或者jetty,resin,tomcat部署后,文件的路径也是有区别的.比如我们在linux上 ...

  3. Java读取Properties配置文件

    目录 1.Properties类与Properties配置文件 2.Properties中的主要方法 3.示例 1.Properties类与Properties配置文件 Properties类继承自H ...

  4. Java删除properties配置文件中指定键值的代码

    将开发过程较好的一些内容片段记录起来,下面的内容段是关于Java删除properties配置文件中指定键值的内容. public static boolean deleteKeyValue4Pro(S ...

  5. JAVA通信编程(二)——如何读取java的properties配置文件(插播)

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  6. java从property中取值,JAVA读取PROPERTIES配置文件

    项目经常用到json,xml,Properties,文本文件等,作为配置文件.用来存储连接字符串或其他配置参数等. 本文记录properties. properties文件,存储格式 键=值.例如新建 ...

  7. properties 配置 java_Java 操作Properties配置文件详解

    1 简介: JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,是使用一种键值对的形式来保存属性集,其中键和值都是字符串类型. java.util.P ...

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

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

  9. 【转载】java读取.properties配置文件的几种方法

    读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的): 一.通过jdk提供的java.util.Properties类. 此类继承自java.uti ...

最新文章

  1. 2021年5月信息系统项目管理师案例分析真题+视频讲解(1)
  2. python 之 click 包,设置命令行参数
  3. DBCP|C3P0参数详解
  4. js纯ajax,自动完成JS类(纯JS, Ajax模式)
  5. faker生成测试数据,一些数据生成方法摘取
  6. codeforces #309 div1 D
  7. 配置vmcore通过ssh转储到其他主机的方法
  8. 为什么字符串中的字符无效_JavaScript中的基本字符串与字符串对象的区别
  9. 御剑后台扫描工具下载
  10. 485转4-20mA信号转换0-10v5v电压电流采集模块
  11. 如何用MAYA 制作人物动画 使人物动作更加真实流畅
  12. 市场营销方式详尽客户调查报告:Inbound vs. Outbound
  13. clion:输出中文乱码终极解决方案
  14. php网站服务器ip地址吗,php 服务器ip地址吗
  15. 搭建手机文件服务器,普通用户的低成本家庭文件服务器(伪NAS)的搭建(手机备份篇)...
  16. Android面试,给正在找工作的安卓攻城师们 ... ...
  17. 此证书的签发者无效解决办法
  18. eclipse将程序打包放到linux运行
  19. 软件流程和管理(二):SDLCs — Process Formal
  20. tensorflow安装2022/3/12

热门文章

  1. 【NOIP2009PJ】细胞分裂
  2. 大数据分析如何跟踪迁移模式
  3. nmn服用后会出现什么副作用,nmn产品副作用及时预防
  4. java超级计算器,jdk自带类
  5. acwing 3496. 特殊年份(蓝桥杯)
  6. 计算机网络设备调试员报名,工信部网络设备调试员一级高级技师、二级技师
  7. jacob不能在Linux系统使用!
  8. 宽带服务器盒信号灯红色闪烁,移动盒子光信号闪红灯怎么回事
  9. 简单易行的用windows系统虚拟苹果mac操作系统 mac下android环境搭建笔记(android studio)
  10. 计算机科学与技术的论文致谢,毕业论文致谢200字(精选7篇)