Java中Properties文件的配置与应用:详解test.properties文件格式与使用场景
Java中的属性文件是一个配置文件,主要用于表达配置信息。文件类型为 *.properties,格式是文本文件,文件的内容是带有“ key = value”格式的格式。在属性中

在文件中,“#”可以用作注释。属性文件用于Java编程中,非常方便操作。
1。属性文件
test.properties
----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -----------------------------------------------
########################################## #########
ICISReport的#Configuration For for Business Report应用程序#
#日期:2006年11月21日#
########################################## #########
#描述:业务系统主题和报告系统ICISReport分开
#可以单独部署到不同的服务器或同一服务
ICISREPROT可以使用任何
#部署并单独运行的Servlet容器或J2EE服务器,也可以是
#通过业务系统的接口呼叫,应用于业务系统库。
#IcisReport的IP
IcisReport.Server.IP = 192.168.3.143
#IcisReport端口
IcisReport.Server.port = 8080
#ICISReport的上下文路径
IcisReport.ContextPath =/IcisReport
----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -----------------------------------------------
属性类的重要方法
属性类存在于小区java.util中,该类别是从Hashtable继承的
1。GetProperty(String键),使用指定键搜索此属性列表中的属性。也就是说,通过参数键,我们获得了与密钥相对应的值。
2。加载(InputStream Instream),从输入流读取属性列表(键和元素对)。通过加载指定文件(例如Test.properties文件)获取文件
文件中的所有键值对。要搜索getProperty(字符串键)。
3。SetProperty(字符串键,字符串值),调用Hashtable的方法。它通过调用基类的PUT方法来设置键值对。
4。商店(outputStream out,字符串注释),以适合使用加载方法加载到属性表中的格式,以在此属性表中列出属性(键和元素)
)写输出流。与加载方法相反,此方法将键值对写入指定的文件。
5。Clear(),清除所有加载的键值对。该方法在基类中提供。
----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -----------------------------------------
2。操作属性文件的Java方法
读取属性文件
propertiesProp = newproperties();
inputStream in = getClass()。getResourCeasStream(“/icisreport.properties”);
prop.load(in);
设置keyValue = prop.keyset();
for(iterator it = keyValue.iterator(); it.hasnext();)
字符串键=(string)it.next();
-----------------------------------------
outputFile = new FileOutputStream(filename);
property.store(outputfile,Description);
outputFile.Close();
----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ------------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -------
class.getResourCeasStream(“/some/pkg/resource.properties”);
classLoader.getResourCeasStream(“ some/pkg/resource.properties”);
java.util.resourcebundle rs = java.util.resourcebundle.getBundle(“ some.pkg.resource”);
rs.getString(“ xiaofei”);
----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ------------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -------
写属性文件
配置savecf = new Configuration();
savecf.setValue(“ min”,“ 10”);
savecf.setValue(“ max”,“ 1000”);
savecf.savefile(“。\ config \ save.perperties”,“ test”);
摘要:Java属性文件需要放在classPath下方,以便程序可以读取它。类路径实际上是Java类或库的存储路径。在Java项目中,属性被放置在
一个课堂文件。在Web应用程序中,最简单的方法是将其放入Web应用程序的Web-Inf \类目录中,或者可以将其放置在其他文件夹中。目前,您需要设置ClassPath环境变量
当时,将此文件夹路径添加到ClassPath变量,以便也可以读取。在这里,您需要对阶级路径有深刻的了解。类Path不是系统环境在系统中故意设置的变化。
Web-Inf \类也是如此,Java项目的类文件目录也是如此。
发送一个例子,亲自查看。
软件包控制;
导入java.io.io.bufferedinputstream;
导入java.io.io.fileinputstream;
导入java.io.io.fileoutputstream;
导入java.io.ioexception;
导入java.io.inputstream;
导入java.io.outputstream;
导入Java.util.Enumeration;
导入java.util.properties;
公共类Testmain {
//根据键读取值
公共静态字符串readValue(字符串filepath,字符串键){
属性props = new Properties();
尝试 {
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
props.load(in);
字符串值= props.getProperty(key);
system.out.println(键+值);
返回值;
} catch(异常E){
e.printstacktrace();
返回null;
//阅读所有属性的信息
公共静态void readproperties(字符串filepath){
属性props = new Properties();
尝试 {
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
props.load(in);
枚举en = props.propertynames();
while(en.hasmoreelements()){
字符串key =(string)en.nextelement();
字符串属性= props.getProperty(key);
system.out.println(键+属性);
} catch(异常E){
e.printstacktrace();
//写属性信息
public static void writeproperties(字符串filepath,string parametername,string parametervalue){
属性prop = new Properties();
尝试 {
InputStream FIS = new FileInputStream(filepath);
//从输入流读取属性列表(键和元素对)
prop.load(FIS);
//调用Hashtable方法。使用GetProperty方法提供并行性。
//强制将字符串用于属性的键和值。返回值是悬念呼叫投票的结果。
outputStream fos = new fileOutputStream(filepath);
prop.setProperty(parametername,parametervalue);
//以适合使用加载方法加载到属性表中的格式,
//在此属性表中将属性列表(键和元素对)写入输出流
prop.store(fos,“ update'” + parametername +“'value”);
} catch(ioexception e){
system.err.println(“访问”+filepath+“用于更新”+parametername+“ value错误”);
公共静态void main(string [] args){
readValue(“ info.properties”,“ url”);
WriteProperties(“ info.properties”,“ age”,“ 21”);
ReadProperties(“ info.properties”);
system.out.println(“ ok”);
发送一个例子,亲自查看。
软件包控制;
导入java.io.io.bufferedinputstream;
导入java.io.io.fileinputstream;
导入java.io.io.fileoutputstream;
导入java.io.ioexception;
导入java.io.inputstream;
导入java.io.outputstream;
导入Java.util.Enumeration;
导入java.util.properties;
公共类Testmain {
//根据键读取值
公共静态字符串readValue(字符串filepath,字符串键){
属性props = new Properties();
尝试 {
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
props.load(in);
字符串值= props.getProperty(key);
system.out.println(键+值);
返回值;
} catch(异常E){
e.printstacktrace();
返回null;
//阅读所有属性的信息
公共静态void readproperties(字符串filepath){
属性props = new Properties();
尝试 {
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
props.load(in);
枚举en = props.propertynames();
while(en.hasmoreelements()){
字符串key =(string)en.nextelement();
字符串属性= props.getProperty(key);
system.out.println(键+属性);
} catch(异常E){
e.printstacktrace();
//写属性信息
public static void writeproperties(字符串filepath,string parametername,string parametervalue){
属性prop = new Properties();
尝试 {
InputStream FIS = new FileInputStream(filepath);
//从输入流读取属性列表(键和元素对)
prop.load(FIS);
//调用Hashtable方法。使用GetProperty方法提供并行性。
//强制将字符串用于属性的键和值。返回值是悬念呼叫投票的结果。
outputStream fos = new FileOutputStream(filepath);
prop.setProperty(parametername,parametervalue);
//以适合使用加载方法加载到属性表中的格式,
//在此属性表中将属性列表(键和元素对)写入输出流
prop.store(fos,“ update'” + parametername +“'value”);
} catch(ioexception e){
system.err.println(“访问”+filepath+“用于更新”+parametername+“ value错误”);
公共静态void main(string [] args){
readValue(“ info.properties”,“ url”);
WriteProperties(“ info.properties”,“ age”,“ 21”);
ReadProperties(“ info.properties”);
system.out.println(“ ok”);