JAVA 操作 properties 配置文件

JAVA 操作 properties 配置文件一 简介 Java 中的 properties 文件是一种纯文本格式的配置文件 主要用于表达配置信息 文件类型为 properties 文件中内容的格式是 键 值 的格式 在 properties 文件中 可以用井号 来作注释 properties 文件在 Java 编程中用到的地方很多 操作很方便 二 Java 的 Properties 类属性映射 propertymap 是一种存储键

一、简介

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

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

二、Java的Properties类

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

它有三个特性:

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

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

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

Properties类继承自Hashtable,如下:

JAVA 操作 properties 配置文件

构造方法:

  • 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读取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.

详细示例:

JAVA 操作 properties 配置文件

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(String language)
  • Locale(String language, String country)
  • Locale(String language, String country, String variant)

创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。

五、中文资源文件的转码 native2ascii

这个工具用法如下:

JAVA 操作 properties 配置文件

如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果了。

JAVA 操作 properties 配置文件

看明白这个了,就不在为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"); } }

参考资料:

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

赞赏

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210165.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 上午8:09
下一篇 2026年3月19日 上午8:10


相关推荐

  • shift后门提权[通俗易懂]

    shift后门提权[通俗易懂]首先,要说的是不论干什麼,强调的是原理所以在这里把后门的原理讲清楚首先,我们要说的是shift这个键连续按5下它会自动弹出个对话框.这个是windows里面自带的功能键连续按5下后操作系统会自动找到c:\windows\system32\下面一个叫sethc.exe的程序,然后会弹出如下对话框[p_w_upload=9]在进程里面我们可以看到这个程序被系统运行…

    2026年1月14日
    5
  • 京东云OpenClaw推出现场免费安装活动

    京东云OpenClaw推出现场免费安装活动

    2026年3月13日
    2
  • redis源码 -ziplist

    注释的翻译:/*Theziplistisaspeciallyencodedduallylinkedlistthatisdesigned*tobeverymemoryefficient.Itstoresbothstringsandintegervalues,*whereintegersareencodedasactualint

    2022年4月10日
    39
  • 使用扣子+飞书+DeepSeek搭建批量提取公众号文章内容并改写的智能体

    使用扣子+飞书+DeepSeek搭建批量提取公众号文章内容并改写的智能体

    2026年3月12日
    2
  • 求逆矩阵的几种方法_求逆矩阵有几种方法

    求逆矩阵的几种方法_求逆矩阵有几种方法**1.待定系数法**矩阵A=1,2-1,-3假设所求的逆矩阵为a,bc,d则这里写图片描述从而可以得出方程组a+2c=1b+2d=0-a-3c=0-b-3d=1解得a=3;b=2;c=-1;d=-12.伴随矩阵求逆矩阵伴随矩阵是矩阵元素所对应的代数余子式,所构成的矩阵,转置后得到的新矩阵。我们先求出伴随矩阵A*=-3,-21,1接下来,求出矩阵A的行列式|A|=1*(-3)-(-1)*2=-3+

    2022年8月21日
    11
  • websocket和http区别

    websocket和http区别目录一 什么是 websocket 二 websocket 的原理三 websocket 与 http 的关系四 websocket 解决的问题 1 http 存在的问题 2 longpoll 长轮询 3 Ajax 轮询 4 websocket 的改进推送延迟 服务端数据发生变更后 长轮询结束 立刻返回响应给客户端 服务端压力 长轮询的间隔期一般很长 例如 30s 60s 并且服务端 hold 住连接不会消耗太多服务端资源 推送延迟 服务端压力 配置一般不会发生变化 频繁的轮询会给服务端造成很大的压力 推送延迟和服务端压力无法中和

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号