impor java.io.*;
import java.util.*;
public class ReadProperties
{
public static void main(String[] args) {
File pFile = new File("e:\test.properties"); // properties文件放在e盘下(windows)
FileInputStream pInStream=null;
try {
pInStream = new FileInputStream(pFile );
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Properties p = new Properties();
try {
p .load(pInStream ); //Properties 对象已生成,包括文件中的数据
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Enumeration enu = p.propertyNames(); //取出所有的key
//输出--1
p.list(System.out) ; //System.out可以改为其他的输出流(包括可以输出到文件)
//输出--2
while( enu .hasMoreElements())
{
System.out.print("key="+enu.nextElement());
System.out.print("value="+p.getProperty((String)enu .nextElement()));
}
}
}
读取xml格式的配置文件
test.xml文件ruxi
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties><pre name="code" class="java">impor java.io.*;
import java.util.*;
public class ReadProperties
{
public static void main(String[] args) {
File pFile = new File("e:\test.properties"); // properties文件放在e盘下(windows)
FileInputStream pInStream=null;
try {
pInStream = new FileInputStream(pFile );
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Properties p = new Properties();
try {
p .load(pInStream ); //Properties 对象已生成,包括文件中的数据
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Enumeration enu = p.propertyNames(); //取出所有的key
//输出--1
p.list(System.out) ; //System.out可以改为其他的输出流(包括可以输出到文件)
//输出--2
while( enu .hasMoreElements())
{
System.out.print("key="+enu.nextElement());
System.out.print("value="+p.getProperty((String)enu .nextElement()));
}
}
}
读取xml格式的配置文件
test.xml文件ruxi
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="koo">bar</entry>
<entry key="fu">baz</entry>
</properties>
读取xml的方法
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) {
File pFile = new File("e:\test.xml"); // properties文件放在e盘下(windows)
FileInputStream pInStream = null;
try {
pInStream = new FileInputStream(pFile);
Properties p = new Properties();
p.loadFromXML(pInStream);
p.list(System.out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
ntry> <entry key="fu">baz</entry> </properties> 读取xml的方法 import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class Test { public static void main(String[] args) { File pFile = new File("e:\test.xml"); // properties文件放在e盘下(windows) FileInputStream pInStream = null; try { pInStream = new FileInputStream(pFile); Properties p = new Properties(); p.loadFromXML(pInStream); p.list(System.out); } catch (IOException e) { e.printStackTrace(); } } }