Java getResourceAsStream的使用方法

jopen 9年前

在java项目中会经常用到getResourceAsStream这个函数获取一些配置文件,但是怎样正确使用这个函数呢?

getResourceAsStream(String path)这个函数的参数是一个路径,但是这个路径应该怎么填?使用该函数的难点也就在此。

其实只要记住一个关键点,getResourceAsStream这个函数寻找文件的起点是JAVA项目编译之后的根目录,比如一般maven项目编译之后根目录都是target/classes这个文件,举个例子,比如下面这个目录树就是一个maven项目编译之后的文件分布情况:
.
├── auth.server.properties
├── com
│   ├── winwill
│   │   └── test
│   │       ├── TestConstants.class
│   │       ├── TestConstants$HOST.class
│   │       ├── TestGetResourceAsStream.class
│   │       └── TestGuava.class
│   └── xiaomi
│       └── xmpush
│           └── ios
│               ├── IOSPushTest.class
│               ├── UploadCertificate$1.class
│               └── UploadCertificate.class
├── config
│   └── config2.properties
├── config.properties
├── kafka-consumer.properties
├── kafka-producer.properties
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│       └── com.winwill.test
│           └── test
│               ├── pom.properties
│               └── pom.xml
├── redis.properties
└── zookeeper.properties

我们在TestGetResourceAsStream.class这个类中应该怎么获取config.properties和config2.properties这两个文件呢?

使用getClass().getResourceAsStream()

从上面的目录树可以看到,config这个文件的路径是/config.properties(/表示根目录),config2.properties这个文件的路径是/config/config2.properties,所以我们可以使用下面的代码获取这两个文件:


package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**   * Created by qifuguang on 14-9-28.   */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 获取config2.properties          InputStream config2 = this.getClass().getResourceAsStream("/config/config2.properties");            // 获取config.properties          InputStream config = this.getClass().getResourceAsStream("/config.properties");      }  }


上面的代码使用的是从根目录开始的绝对路径,那么可不可以使用相对路径呢?当然可以,请看下面的代码:


package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**   * Created by qifuguang on 14-9-28.   */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 获取config2.properties          InputStream config2 = this.getClass().getResourceAsStream("../../../config/config2.properties");            // 获取config.properties          InputStream config = this.getClass().getResourceAsStream("../../../config.properties");          System.out.println(config + " " + config2);      }  }

代码中的两个路径就是这两个文件相对TestGetResourceAsStream.class这个文件的的相对路径。


使用getClass().getClassLoader().getResourceAsStream()

getClass().getClassLoader().getResourceAsStream()默认使用的路径就是class文件的根目录,所以使用getClass().getClassLoader().getResourceAsStream()来获取文件的时候不能在路径前面加上/,于是我们可以这样获取这两个文件:


package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**   * Created by qifuguang on 14-9-28.   */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 获取config2.properties          InputStream config2 = this.getClass().getClassLoader().getResourceAsStream("config/config2.properties");            // 获取config.properties          InputStream config = this.getClass().getClassLoader().getResourceAsStream("config.properties");      }  }