蘑菇街优店怎么找:Configuration -Properties 文件
来源:百度文库 编辑:九乡新闻网 时间:2024/05/05 19:21:09
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 文件
Properties类,访问属性文件的方式
不错的编写properties文件的Eclipse插件
JRI configuration
Properties操作示例
SharePoint setup and configuration
Configuration file and brightenss
安装Properties Editor
PIC18 configuration settings--MCC编译器
started the wireless zero configuration service
Curbs in more cities on residential properties
注意数据库配置文件 jdbc.properties 包含空格
Advanced Configuration Tool 系统配置超级工具
Armed men attack public, private properties i...
log4j.properties详解与例子,Java实例教程,Java系列教程,Java
mineral properties翻译成矿业权的详细来源--cnki翻译助手
Installing a large Linux cluster, Part 1: Introduction and hardware configuration
Installing a large Linux cluster, Part 2: Management server configuration and node installation
Installing a large Linux cluster, Part 4: Node installation and GPFS cluster configuration
[转帖]恺撒里兹酒店管理学院毕业生李斯专访--《People&Properties》专访
电气文件
文件操作
bat文件
恢复文件