如果文件在编译后被放置在目标/类下,则它已经在构建路径中的目录中。目录src / main /
resources是此类资源的Maven默认目录,并且Eclipse Maven插件(M2E)自动将其放置到构建路径中。因此,无需移动属性文件。
另一个主题是如何检索此类资源。构建路径中的资源自动位于正在运行的Java程序的类路径中。考虑到这一点,您应该始终使用类加载器加载此类资源。示例代码:
String resourceName = "myconf.properties"; // could also be a constantClassLoader loader = Thread.currentThread().getContextClassLoader();Properties props = new Properties();try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) { props.load(resourceStream);}// use props here ...


