無論是Servlet或者Spring哆致、MyBatis绕德,配置資源文件都是必不可少的一項(xiàng)工作,Java中主要提供了提供了2個(gè)類來讀取資源文件摊阀,一個(gè)是Class類耻蛇,一個(gè)是ClassLoader類。 本文對Java中讀取資源文件做一個(gè)總結(jié)胞此,希望對大家有所幫助臣咖。
本篇中講解以Maven為例,項(xiàng)目結(jié)構(gòu)如下:
1.png
一漱牵、Java API
java.lang.Class
和 java.lang.ClassLoader
都提供了
InputStream getResourceAsStream(String name);
和
java.net.URL getResource(String name)
方法來獲取資源文件夺蛇。
二、相對路徑和絕對路徑
** 相對路徑 ** :path中不以'/'開頭表示該路徑是相對路徑酣胀,相對于當(dāng)前類所在的目錄刁赦。
** 絕對路徑 ** :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下的資源文件
我們現(xiàn)在想要讀取 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);
五铆农、注意事項(xiàng)
因?yàn)镸aven默認(rèn)不會(huì)將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;
}
}