我正在使用资源文件中的动态名称动态创建一堆属性文件,为此我使用以下代码
File file = new File("src/main/resources/" + fileName+ ".properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, fileName);
fileOut.close();
问题: - 每次我都必须手动刷新资源文件才能访问动态创建的属性文件。我如何解决这个问题?
答案 0 :(得分:1)
我通过在类路径中存储资源文件来解决这个问题,这样类加载器就可以加载动态添加的文件。
public static void addResourceURLIntoClassPath(URL u) throws IOException {
URLClassLoader urlLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(urlLoader, new Object[] { u });
} catch (Throwable t) {
t.printStackTrace();
}
}
此处URL是src / main / resources
的路径答案 1 :(得分:0)
更好的解决方案是使用apache-commons配置项目。它提供更清洁和测试的apis。以下是关于如何使用它的link。