Configuration -Properties 文件
来源:百度文库 编辑:神马文学网 时间:2024/07/08 21:54:30
Properties文件
Properties 文件是配置程序的一种常用作法. Commons Configuration当然支持这种格式并且增强 java.util.Properties 这个类 这一节就是介绍PropertiesConfiguration 类的一些特性。
Loading 加载
At 首先,假设有个应用程序的所有配置信息都在一个叫“usergui.properties ”的properties 文件里,文件的内容如下:
# Properties definining the GUI
colors.background = #FFFFFF
要加载这个文件,你可以像下面这样写:
Configuration config = new PropertiesConfiguration("usergui.properties");
If 如果你可以使用文件的绝对路径,那么这个文件会自动在下面几个地方被搜寻:
当前目录
用户主目录
classpath
Includes 包含文件
If a 如果property文件里有叫做 "include"的键值对,并且值是一个 property文件名, 这么个文件也将被自动包含进配置信息中,举例如下:
# usergui.properties
include = colors.properties
include = sizes.properties
# colors.properties
colors.background = #FFFFFF
Automatic Reloading 自动重新加载
A common issue with properties file is to handle the reloading of the file when it changes. 通常你需要开启一个线程来监视配置文件的时间,并在文件被修改后重新加载进来。 Commons Configuration集成了这个加载机制, 如果需要使用自动加载,只需要在年id配置信息里声明一个自动重载策略:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
现在你随时手动修改了usergui.properties, 配置信息都能够自动刷新,修改后的值立即在程序里生效。
Saving 保存
调用save()方法就可以保存你的配置:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);
config.save();
你也可以复制一份配置保存到另外一个文件:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);
config.save("usergui.backup.properties);
如果你不想在配置信息改变之后费心的手动保存文件,你可以激活自动保存模式::
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setAutoSave(true);
config.setProperty("colors.background", "#000000); // the configuration is saved after this call
Lists and arrays
Commons Configuration 可以很轻松的返回一组值, 例如你的文件包含了用逗号分割的一组数据:
# chart colors
colors.pie = #FF0000, #00FF00, #0000FF
你不用手动拆分字符串,可以直接作为数组返回:
String[] colors = config.getStringArray("colors.pie");
另外一种作法, 你可以通过多行的同名键值对,来返回一组值。
# chart colors
colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;
变量窜改
如果你熟悉Ant或者Maven,你肯定已经用到 (像 ${token})这样可以在配置文件被加载时自动扩充的变量。 Commons Configuration 也支持这样的特性, 下面是个示例:
application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}
特殊字符
如果你需要用到一些特殊字符,比如换行符,制表符,或者unicode字符,你需要进行字符转义,字符串分隔符(默认的是逗号“,”)也需要被转义 :
key = This \n string \t contains \, escaped \\ characters \u0020原文地址:[url=http://jakarta.apache.org/commons/configuration/howto_properties.html]http://jakarta.apache.org/commons/configuration/howto_properties.html[/url] ( lizongbo @ gmail.com) Properties文件 Properties 文件是配置程序的一种常用作法. Commons Configuration当然支持这种格式并且增强 java.util.Properties 这个类 这一节就是介绍PropertiesConfiguration 类的一些特性。 Loading 加载 At 首先,假设有个应用程序的所有配置信息都在一个叫“usergui.properties ”的properties 文件里,文件的内容如下: # Properties definining the GUI colors.background = #FFFFFF 要加载这个文件,你可以像下面这样写: Configuration config = new PropertiesConfiguration("usergui.properties"); If 如果你可以使用文件的绝对路径,那么这个文件会自动在下面几个地方被搜寻: 当前目录 用户主目录 classpath Includes 包含文件 If a 如果property文件里有叫做 "include"的键值对,并且值是一个 property文件名, 这么个文件也将被自动包含进配置信息中,举例如下: # usergui.properties include = colors.properties include = sizes.properties # colors.properties colors.background = #FFFFFF Automatic Reloading 自动重新加载 A common issue with properties file is to handle the reloading of the file when it changes. 通常你需要开启一个线程来监视配置文件的时间,并在文件被修改后重新加载进来。 Commons Configuration集成了这个加载机制, 如果需要使用自动加载,只需要在年id配置信息里声明一个自动重载策略: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setReloadingStrategy(new FileChangedReloadingStrategy()); 现在你随时手动修改了usergui.properties, 配置信息都能够自动刷新,修改后的值立即在程序里生效。 Saving 保存调用save()方法就可以保存你的配置: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000); config.save(); 你也可以复制一份配置保存到另外一个文件: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000); config.save("usergui.backup.properties); 如果你不想在配置信息改变之后费心的手动保存文件,你可以激活自动保存模式:: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setAutoSave(true); config.setProperty("colors.background", "#000000); // the configuration is saved after this call Lists and arrays Commons Configuration 可以很轻松的返回一组值, 例如你的文件包含了用逗号分割的一组数据: # chart colors colors.pie = #FF0000, #00FF00, #0000FF 你不用手动拆分字符串,可以直接作为数组返回: String[] colors = config.getStringArray("colors.pie"); 另外一种作法, 你可以通过多行的同名键值对,来返回一组值。 # chart colors colors.pie = #FF0000; colors.pie = #00FF00; colors.pie = #0000FF; 变量窜改如果你熟悉Ant或者Maven,你肯定已经用到 (像 ${token})这样可以在配置文件被加载时自动扩充的变量。 Commons Configuration 也支持这样的特性, 下面是个示例: application.name = Killer App application.version = 1.6.2 application.title = ${application.name} ${application.version} 特殊字符如果你需要用到一些特殊字符,比如换行符,制表符,或者unicode字符,你需要进行字符转义,字符串分隔符(默认的是逗号“,”)也需要被转义 : key = This \n string \t contains \, escaped \\ characters \u0020
Properties 文件是配置程序的一种常用作法. Commons Configuration当然支持这种格式并且增强 java.util.Properties 这个类 这一节就是介绍PropertiesConfiguration 类的一些特性。
Loading 加载
At 首先,假设有个应用程序的所有配置信息都在一个叫“usergui.properties ”的properties 文件里,文件的内容如下:
# Properties definining the GUI
colors.background = #FFFFFF
要加载这个文件,你可以像下面这样写:
Configuration config = new PropertiesConfiguration("usergui.properties");
If 如果你可以使用文件的绝对路径,那么这个文件会自动在下面几个地方被搜寻:
当前目录
用户主目录
classpath
Includes 包含文件
If a 如果property文件里有叫做 "include"的键值对,并且值是一个 property文件名, 这么个文件也将被自动包含进配置信息中,举例如下:
# usergui.properties
include = colors.properties
include = sizes.properties
# colors.properties
colors.background = #FFFFFF
Automatic Reloading 自动重新加载
A common issue with properties file is to handle the reloading of the file when it changes. 通常你需要开启一个线程来监视配置文件的时间,并在文件被修改后重新加载进来。 Commons Configuration集成了这个加载机制, 如果需要使用自动加载,只需要在年id配置信息里声明一个自动重载策略:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
现在你随时手动修改了usergui.properties, 配置信息都能够自动刷新,修改后的值立即在程序里生效。
Saving 保存
调用save()方法就可以保存你的配置:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);
config.save();
你也可以复制一份配置保存到另外一个文件:
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);
config.save("usergui.backup.properties);
如果你不想在配置信息改变之后费心的手动保存文件,你可以激活自动保存模式::
PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setAutoSave(true);
config.setProperty("colors.background", "#000000); // the configuration is saved after this call
Lists and arrays
Commons Configuration 可以很轻松的返回一组值, 例如你的文件包含了用逗号分割的一组数据:
# chart colors
colors.pie = #FF0000, #00FF00, #0000FF
你不用手动拆分字符串,可以直接作为数组返回:
String[] colors = config.getStringArray("colors.pie");
另外一种作法, 你可以通过多行的同名键值对,来返回一组值。
# chart colors
colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;
变量窜改
如果你熟悉Ant或者Maven,你肯定已经用到 (像 ${token})这样可以在配置文件被加载时自动扩充的变量。 Commons Configuration 也支持这样的特性, 下面是个示例:
application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}
特殊字符
如果你需要用到一些特殊字符,比如换行符,制表符,或者unicode字符,你需要进行字符转义,字符串分隔符(默认的是逗号“,”)也需要被转义 :
key = This \n string \t contains \, escaped \\ characters \u0020原文地址:[url=http://jakarta.apache.org/commons/configuration/howto_properties.html]http://jakarta.apache.org/commons/configuration/howto_properties.html[/url] ( lizongbo @ gmail.com) Properties文件 Properties 文件是配置程序的一种常用作法. Commons Configuration当然支持这种格式并且增强 java.util.Properties 这个类 这一节就是介绍PropertiesConfiguration 类的一些特性。 Loading 加载 At 首先,假设有个应用程序的所有配置信息都在一个叫“usergui.properties ”的properties 文件里,文件的内容如下: # Properties definining the GUI colors.background = #FFFFFF 要加载这个文件,你可以像下面这样写: Configuration config = new PropertiesConfiguration("usergui.properties"); If 如果你可以使用文件的绝对路径,那么这个文件会自动在下面几个地方被搜寻: 当前目录 用户主目录 classpath Includes 包含文件 If a 如果property文件里有叫做 "include"的键值对,并且值是一个 property文件名, 这么个文件也将被自动包含进配置信息中,举例如下: # usergui.properties include = colors.properties include = sizes.properties # colors.properties colors.background = #FFFFFF Automatic Reloading 自动重新加载 A common issue with properties file is to handle the reloading of the file when it changes. 通常你需要开启一个线程来监视配置文件的时间,并在文件被修改后重新加载进来。 Commons Configuration集成了这个加载机制, 如果需要使用自动加载,只需要在年id配置信息里声明一个自动重载策略: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setReloadingStrategy(new FileChangedReloadingStrategy()); 现在你随时手动修改了usergui.properties, 配置信息都能够自动刷新,修改后的值立即在程序里生效。 Saving 保存调用save()方法就可以保存你的配置: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000); config.save(); 你也可以复制一份配置保存到另外一个文件: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000); config.save("usergui.backup.properties); 如果你不想在配置信息改变之后费心的手动保存文件,你可以激活自动保存模式:: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setAutoSave(true); config.setProperty("colors.background", "#000000); // the configuration is saved after this call Lists and arrays Commons Configuration 可以很轻松的返回一组值, 例如你的文件包含了用逗号分割的一组数据: # chart colors colors.pie = #FF0000, #00FF00, #0000FF 你不用手动拆分字符串,可以直接作为数组返回: String[] colors = config.getStringArray("colors.pie"); 另外一种作法, 你可以通过多行的同名键值对,来返回一组值。 # chart colors colors.pie = #FF0000; colors.pie = #00FF00; colors.pie = #0000FF; 变量窜改如果你熟悉Ant或者Maven,你肯定已经用到 (像 ${token})这样可以在配置文件被加载时自动扩充的变量。 Commons Configuration 也支持这样的特性, 下面是个示例: application.name = Killer App application.version = 1.6.2 application.title = ${application.name} ${application.version} 特殊字符如果你需要用到一些特殊字符,比如换行符,制表符,或者unicode字符,你需要进行字符转义,字符串分隔符(默认的是逗号“,”)也需要被转义 : key = This \n string \t contains \, escaped \\ characters \u0020
Configuration -Properties 文件
使Struts 中的 properties属性的文件支持中文的插件的安装方法
workers.properties
jk2.properties
Apache log4net Manual: Configuration
XML and Software Configuration
SQL Server Configuration Manager
toolbox.xilinx SelectMAP Configuration Mode
jk2.properties(OK)
struts2.0 struts.properties
workers.properties说明
log4j.properties 使用
struts2中struts.properties
Java Properties load 函数
Hamachi : Stay Connected Zero-configuration VPN
Struts2 Unable to load configuration. - bean ...
[Laskey99] Section 5.7. Manual Network Configuration
The Eclipse Tabbed Properties View
详解struts2中struts.properties
详解struts2中struts.properties
Properties of Preferential Election Rules
[Bernstein09] Section 5.4. Transactional Properties
log4j.properties的放置位置
程序 解决 Properties 中文问题