maven工程有专门的resources文件夹存储动态资源,但有时咱们有将动态文件与class并列的状况,此时须要在maven中增加如下配置:

<resources>    <resource>        <directory>src/main/java</directory>        <includes>            <include>**/*.properties</include>        </includes>    </resource>    <resource>        <directory>src/resources</directory>    </resource></resources>

示意蕴含动态文件的门路。
另,对于java读取动态资源,分为绝对路径相对路径两种形式
绝对路径示意以src为根目录,以“/”为起始字符的文件内容。而相对路径示意与以后类并列的地位,不须要加“/”。

private static String PROPERTIESFILE = "conn.properties";public static void main(String[] args) throws IOException {    Pool pool = new Pool();    pool.load();}private void load() throws IOException {    // 相对路径    InputStream is = this.getClass().getResourceAsStream("" + PROPERTIESFILE);    // 绝对路径    InputStream is = this.getClass().getResourceAsStream("/" + PROPERTIESFILE);    // 另一种形式 - 此办法会遍历门路寻找,所以不须要加“/”    InputStream is = ClassLoader.getSystemResourceAsStream(PROPERTIESFILE);    Properties p = new Properties();    p.load(is);    System.out.println(p.getProperty("hello"));}