| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 742 人关注过本帖
标题:[分享]Properties属性文件学习,偶的一些学习代码
只看楼主 加入收藏
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
 问题点数:0 回复次数:2 
[分享]Properties属性文件学习,偶的一些学习代码

//从绝对路径取参数
import java.io.*;
import java.util.*;


public class ConfigInfo {

//根据key读取value
public static String readValue(String filePath, String key){
Properties props = new Properties();
try{
//InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(new FileInputStream(filePath));
String value = props.getProperty(key);
return value;
in.close();
}catch(Exception e){
e.printStackTrace();
}
}

//读取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);
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
}

//写入properties信息
public static void writeProperties(String filePath, String parameterName, String parameterValue){
Properties props = new Properties();
try{
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
props.load(fis);
fis.close();
//调用Hashtable的方法put。使用getProperty方法提供并行性
//强制要求为属性的键和值使用字符串。返回值是Hashtable调用put的结果
OutputStream fos = new FileOutputStream(filePath);
props.setProperty(parameterName, parameterValue);
//以适合使用load方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素对)写入输出
props.store(fos,"");
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

搜索更多相关主题的帖子: 文件 Properties props 属性 String 
2007-07-07 14:38
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
得分:0 

[转载]
一、读取Properties属性文件信息

test.txt

name=bitan
password=bitan
time=2004-11-7


GetPropertiesDemo.java:
package test.properties;


GetPropertiesDemo.java:
package test.properties;

import java.io.*;
import java.util.Properties;


public class GetPropertiesDemo {

public static void main(String[] args) throws IOException {
Class c = new GetPropertiesDemo().getClass();
InputStream is = c.getResourceAsStream("test.txt");
Properties prop = new Properties();
prop.load(is);
String name = prop.getProperty("name");
String password = prop.getProperty("password");
String time = prop.getProperty("time");
System.out.print(name + " " + password + " " + time);

is.close();
}
}


结果:

bitan bitan 2004-11-7

二、利用Properties读取和写入XML文件信息

bitan.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>
<properties>
<entry key="name">bitan</entry>
<entry key="age">20</entry>
<entry key="sex">male</entry>
<entry key="address">Changde Hunan China</entry>
</properties>

Tiger16.java


package tigers;

import java.io.*;
import java.util.*;
public class Tiger16 {
public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.loadFromXML(new FileInputStream("bitan.xml"));
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String sex = prop.getProperty("sex");
String address = prop.getProperty("address");
System.out.println(name + ", " + age + ", " + sex + ", " + address);
prop.list(System.out);
prop.storeToXML(new FileOutputStream("bitan2.xml"), "a simple test", "utf-8");
prop = new Properties();
prop.setProperty("name", "helen");
prop.setProperty("age", "22");
prop.setProperty("sex", "female");
prop.setProperty("address", "Hangzhou Zhejiang China");
prop.storeToXML(new FileOutputStream("helen.xml"), "helen's information.");
} catch (Exception e) {
e.printStackTrace();
}
}
}


package tigers;

import java.io.*;
import java.util.*;
public class Tiger16 {
public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.loadFromXML(new FileInputStream("bitan.xml"));
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String sex = prop.getProperty("sex");
String address = prop.getProperty("address");
System.out.println(name + ", " + age + ", " + sex + ", " + address);
prop.list(System.out);
prop.storeToXML(new FileOutputStream("bitan2.xml"), "a simple test", "utf-8");
prop = new Properties();
prop.setProperty("name", "helen");
prop.setProperty("age", "22");
prop.setProperty("sex", "female");
prop.setProperty("address", "Hangzhou Zhejiang China");
prop.storeToXML(new FileOutputStream("helen.xml"), "helen's information.");
} catch (Exception e) {
e.printStackTrace();
}
}
}


结果:

bitan, 20, male, Changde Hunan China
-- listing properties --
address=Changde Hunan China
age=20
name=bitan
sex=male

bitan2.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>a simple test</comment>
<entry key="address">Changde Hunan China</entry>
<entry key="age">20</entry>
<entry key="name">bitan</entry>
<entry key="sex">male</entry>
</properties>


helen.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>helen's information.</comment>
<entry key="address">Hangzhou Zhejiang China</entry>
<entry key="age">22</entry>
<entry key="name">helen</entry>
<entry key="sex">female</entry>
</properties>

结论:

可以利用这个途径快速存取一些比较简短而又重要的信息,替代或补充数据库。

结论:

可以利用这个途径快速存取一些比较简短而又重要的信息,替代或补充数据库。

三、利用Properties读取和写入普通文件信息


package tigers;

import java.io.*;
import java.util.*;


public class Tiger17 {

public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.setProperty("name", "bitan");
prop.setProperty("age", "24");
prop.setProperty("sex", "male");
prop.store(new FileOutputStream("bitan.txt"), "bitan's information");
prop.load(new FileInputStream("bitan.txt"));
prop.list(System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}


结果:

-- listing properties --
age=24
name=bitan
sex=male

bitan.txt

#bitan's information
#Wed Feb 09 13:49:34 CST 2005
age=24
name=bitan
sex=male

四、利用Properties获取操作系统信息


package tigers;

import java.util.*;


public class Tiger15 {

public static void main(String[] args) {
Properties prop = new Properties(System.getProperties());
prop.list(System.out);
}
}


结果:

-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=K:\tiger\jre\bin
java.vm.version=1.5.0_01-b08
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=CN
sun.os.patch.level=Service Pack 1
java.vm.specification.name=Java Virtual Machine Specification
user.dir=G:\workspace\tigetest
java.runtime.version=1.5.0_01-b08
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=K:\tiger\jre\lib\endorsed
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\bitan\LOCALS~1\Temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=GBK
java.library.path=K:\tiger\jre\bin;.;C:\WINDOWS\System3...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\bitan
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=GBK
java.specification.version=1.5
user.name=bitan
java.class.path=G:\workspace\tigetest\bin
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=K:\tiger\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_01
java.ext.dirs=K:\tiger\jre\lib\ext
sun.boot.class.path=K:\tiger\jre\lib\rt.jar;K:\tiger\jre\...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=321246


2007-07-07 14:39
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
收藏
得分:0 

怎么没人回复呀!


2007-07-07 21:32
快速回复:[分享]Properties属性文件学习,偶的一些学习代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024783 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved