Java中读取资源文件小结

无论是Servlet或者Spring、MyBatis,配置资源文件都是必不可少的一项工作,Java中主要提供了提供了2个类来读取资源文件,一个是Class类,一个是ClassLoader类。 本文对Java中读取资源文件做一个总结,希望对大家有所帮助。

本篇中讲解以Maven为例,项目结构如下:

1.png

一、Java API

java.lang.Classjava.lang.ClassLoader 都提供了
InputStream getResourceAsStream(String name);
java.net.URL getResource(String name)
方法来获取资源文件。

二、相对路径和绝对路径

** 相对路径 ** :path中不以'/'开头表示该路径是相对路径,相对于当前类所在的目录。
** 绝对路径 ** :path中以'/'开头表示该路径是绝对路径,相对于classpath的绝对路径。

三、读取resources目录下的资源文件

1. 读取resources目录下的config.properties文件

    String path = "/config.properties";
    InputStream in = PropertiesLoader.class.getResourceAsStream(path);
    Properties props = load(in);
    System.out.println("path: "+ path + ", props: " + props);

或者使用ClassLoader:

    String path = "/config.properties";
    InputStream in = PropertiesLoader.class.getClassLoader().getResourceAsStream(fileName); 
    Properties props = load(in);
    System.out.println("path: "+ path + ", props: " + props);

四、读取类package下的资源文件

我们现在想要读取 com.mindflow.demo.cfg包下的app.properties文件,代码如下:

        String path = "cfg/app.properties";
    InputStream in = PropertiesLoader.class.getResourceAsStream(path);
    Properties props = load(in);
    System.out.println("path: "+ path + ", props: " + props);

也可以使用绝对路径:

        String path = "/com/mindflow/demo/cfg/app.properties";
    InputStream in = PropertiesLoader.class.getResourceAsStream(path);
    Properties props = load(in);
    System.out.println("path: "+ path + ", props: " + props);

五、注意事项

因为Maven默认不会将src路径下的资源文件打包到classpath路径下,所以需要在pom.xml下增加如下配置:

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

完整配置如下:

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <testSource>${java.version}</testSource>
          <testTarget>${java.version}</testTarget>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
      <!--source code-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

PropertiesLoader.java 源码:

package com.mindflow.demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public abstract class PropertiesLoader {

    public static void main( String[] args ) throws IOException {

        //1.读取resources目录下文件
        String path = "/config.properties";
        Properties props = PropertiesLoader.readFile(path);
        System.out.println("path: "+ path + ", props: " + props);

        //2. 读取resources/data
        path = "/data/db.properties";
        props = PropertiesLoader.readFile(path);
        System.out.println("path: "+ path + ", props: " + props);

        System.out.println("=====================================");

        //3.读取类路径下(src) 资源文件
        path = "cfg/app.properties";
        props = PropertiesLoader.readFile(path);
        System.out.println("path: "+ path + ", props: " + props);

        //4.使用绝对路径读取资源文件
        path = "/com/mindflow/demo/cfg/app.properties";
        props = PropertiesLoader.readFile(path);
        System.out.println("path: "+ path + ", props: " + props);

    }

    public static Properties readFile(String path) throws IOException {
        InputStream in = PropertiesLoader.class.getResourceAsStream(path);
        Properties props = load(in);
        return props;
    }

    public static Properties load(InputStream in) throws IOException {
        Properties props = new Properties();
        props.load(in);
        return props;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,644评论 25 709
  • 感赏儿子在我进家门的第一时间递我一杯水,然后说:妈妈辛苦啦! 感赏儿子把自己的碗筷洗的很干净! 感赏儿子今天跟我谈...
    晓莉_f92b阅读 198评论 1 3
  • 立秋一过,风就来了,悄悄地扫过,慢慢地,从北向南,一点一点地,移动。 于是,地里的高粱红了,玉米黄了,稻谷弯腰了。...
    韩秀琴cn阅读 620评论 2 8
  • 手捧书卷 看不进一个字 并不是因为我太疲倦 也不是这书太乏味 当你的样子映入眼帘 当你的话语来到耳边 我便难以自持...
    王不烦阅读 122评论 0 1