Java访问配置文件实现类
发布时间:2010/11/8 15:34:02 来源:城市学习网 编辑:ziteng
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
public class ConfigurationRead {
private String PFILE = "";
private Properties m_props = null;
public ConfigurationRead(String filePath) {
this.PFILE=filePath;
}
public boolean loadFile() {
try {
m_props = new Properties();
m_props.load(new FileInputStream(getFile()));
return true;
}
catch (URISyntaxException e)
{ System.err.println("文件路径不正确");
e.printStackTrace();
}
catch (Exception e)
{ System.err.println("文件读取异常");
e.printStackTrace();
}
return false;
}
private File getFile() throws URISyntaxException
{
URI fileUri = this.getClass().getClassLoader().getResource(PFILE).toURI();
File m_file = new File(fileUri); return m_file;
}
public String getConfigItem(String name) {
m_props.clear();
try {
m_props.load(new FileInputStream(getFile()));
}
catch (Exception e) {
System.err.println("文件重新读取异常");
e.printStackTrace();
}
String val = m_props.getProperty(name); return val;
}
}