在java web开发中,常常需要在启动服务器的时候就需要读取一些配置文件的信息,以初始化上下文和一些项目用到的配置常量,下面介绍几种读取配置文件的方法:
方法一:
public class PropUtil {
public static Properties getProUtil() {
Properties config = new Properties();
InputStream is = null;
try {
System.out.println(PropUtil.class.getClassLoader().getResource("").toString()
);
System.out.println(PropUtil.class.getResource("/").toString()
);
is = PropUtil.class.getClassLoader().getResourceAsStream(
"quartz.properties
");
config.load(is);
} catch (IOException ex) {
//
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
// ignore
}
}
}
return config;
}
public static void main(String[] args) {
System.out.println(getProUtil().toString());
}
}
输入结果:
file:/D:/all_project/tee_7.6/tee-biz/target/test-classes/ file:/D:/all_project/tee_7.6/tee-biz/target/test-classes/ {org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool, org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore, org.quartz.scheduler.instanceId=one, org.quartz.threadPool.threadPriority=4, org.quartz.scheduler.instanceName=myScheduler, org.quartz.threadPool.threadCount=30}
注意: 在工程项目中,PropUtil源文件所在的路径和编译后的字节码所在的路径不是在一个地方,具体是在pom.xml中配置的,根据输出结果,PropUtil生成的class文件在D:\all_project\tee_7.6\tee-biz\target\test-classes\com\taobao\tee\readResouceFiles目录下,它能读取的资源文件必须放在 D:/all_project/tee_7.6/tee-biz/target/test-classes/ 目录下,所以只有把quartz.properties放在这个目录下才能读取到这个资源文件。
另外,其中最好的写法是:InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("quartz.properties");
方法二:通过文件操作的方式
public class PropUtil {
public static Properties getProUtil() {
Properties config = new Properties();
InputStream is = null;
try {
File file =new File("D:\\all_project\\riskm9.19\\riskm-web\\src\\main\\resources\\quartz.properties");
is=new FileInputStream(file);
config.load(is);
} catch (IOException ex) {
//
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
// ignore
}
}
}
return config;
}
public static void main(String[] args) {
System.out.println(getProUtil().toString());
}
}
方法三:通过资源捆绑的形式:ResourceBundle 。
方法四:其实跟之前差不多:
public void initialize() throws SchedulerException {
// short-circuit if already initialized
if (cfg != null) {
return;
}
if (initException != null) {
throw initException;
}
String requestedFile = System.getProperty(PROPERTIES_FILE);
String propFileName = requestedFile != null ? requestedFile
: "quartz.properties";
File propFile = new File(propFileName);
Properties props = new Properties();
InputStream in = null;
try {
if (propFile.exists()) {
try {
if (requestedFile != null) {
propSrc = "specified file: '" + requestedFile + "'";
} else {
propSrc = "default file in current working dir: 'quartz.properties'";
}
in = new BufferedInputStream(new FileInputStream(propFileName));
props.load(in);
} catch (IOException ioe) {
initException = new SchedulerException("Properties file: '"
+ propFileName + "' could not be read.", ioe);
throw initException;
}
} else if (requestedFile != null) {
in =
Thread.currentThread().getContextClassLoader().getResourceAsStream(requestedFile);
if(in == null) {
initException = new SchedulerException("Properties file: '"
+ requestedFile + "' could not be found.");
throw initException;
}
propSrc = "specified file: '" + requestedFile + "' in the class resource path.";
in = new BufferedInputStream(in);
try {
props.load(in);
} catch (IOException ioe) {
initException = new SchedulerException("Properties file: '"
+ requestedFile + "' could not be read.", ioe);
throw initException;
}
} else {
propSrc = "default resource file in Quartz package: 'quartz.properties'";
ClassLoader cl = getClass().getClassLoader();
if(cl == null)
cl = findClassloader();
if(cl == null)
throw new SchedulerConfigException("Unable to find a class loader on the current thread or class.");
in = cl.getResourceAsStream(
"quartz.properties");
if (in == null) {
in = cl.getResourceAsStream(
"/quartz.properties");
}
if (in == null) {
in = cl.getResourceAsStream(
"org/quartz/quartz.properties");
}
if (in == null) {
initException = new SchedulerException(
"Default quartz.properties not found in class path");
throw initException;
}
try {
props.load(in);
} catch (IOException ioe) {
initException = new SchedulerException(
"Resource properties file: 'org/quartz/quartz.properties' "
+ "could not be read from the classpath.", ioe);
throw initException;
}
}
} finally {
if(in != null) {
try { in.close(); } catch(IOException ignore) { /* ignore */ }
}
}
}