读取配制文件的时候,使用下面的方法,可方便获取classpath绝对路径:
Thread.currentThread().getContextClassLoader().getResource("").getPath();
Thread.currentThread().getContextClassLoader().getResource("").getPath();
propertie = new Properties();
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
try {
inputFile = new FileInputStream(path + XXX.properties);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件失败");
ex.printStackTrace();
}
本文介绍了一种在Java中加载配置文件的方法,并提供了详细的代码示例。通过使用当前线程的上下文类加载器,可以方便地获取到类路径下的配置文件路径。

9661





