项目开发中经常要读取Properties,XML,INI文件,我把我以前开发中用到的一些例子给拿出来,方便大家学习之用!大家在使用过程中有什么疑问,请给我留言,谢谢!
timsmanual.properties文件:manual=AA,BB,CC
ReadProperties.java://读取Properties文件的类
package com.manual;
import java.io.*;
import java.util.*;
public class ReadProperties {
public String getProperties() {
String path = System.getProperty("user.dir");
System.out.println(path);
String propertiesvalue = null;
Properties initProps = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream("timsmanual.properties");//要读取的properties文件
initProps.load(in);
}
catch (Exception e) {
System.err.println("Error reading timsmanual properties "
+ "in JiveGlobals");
e.printStackTrace();
}
finally {
try {
if (in != null) { in.close(); }
} catch (Exception e) {}
}
if (initProps != null) {
propertiesvalue = initProps.getProperty("manual");
if (propertiesvalue != null) {
propertiesvalue = propertiesvalue.trim();
}
}
System.out.println(propertiesvalue);
return propertiesvalue;
}
TestReadProperties.java //测试类
package com.manual;
public class TestReadProperties {
/**
* @param args
*/
public static void main(String[] args) {
ReadProperties a = new ReadProperties();
String path = System.getProperty("user.dir");
System.out.println("path :" + path);
a.getProperties();
}
}
}