无论是Servlet或者Spring、MyBatis,配置资源文件都是必不可少的一项工作,Java中主要提供了提供了2个类来读取资源文件,一个是Class类,一个是ClassLoader类。 本文对Java中读取资源文件做一个总结,希望对大家有所帮助。
本篇中讲解以Maven为例,项目结构如下:
一、Java API
java.lang.Class
和 java.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;
}
}