//从绝对路径取参数
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属性文件信息
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
----------------解决方案--------------------------------------------------------
怎么没人回复呀!
----------------解决方案--------------------------------------------------------