注:在jar包中直接使用路径方式直接读Resource目录下的文件比较难,建议使用流的方式读文件。
1 工程目录
2 源代码
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import java.io.*;
public class TestController {
public String readFile() throws IOException {
String path = "config/data";
System.out.println("(一) 读取jar包文件中的数据流");
// (一) 读取jar包文件中的数据流
// (1) 使用getClass()取得当前对象所属的Class对象,注意需要再path前面加”/“
// 以’/'开头是从项目的ClassPath根下获取资源,不以’/‘开头是从此类所在的包下获取资源。
InputStream classIS = this.getClass().getResourceAsStream("/"+path);
System.out.print("1 getClass:");
System.out.println(classIS);
// (2) 使用getClassLoader()获得该Class对象的类装载器
InputStream classLoaderIS = this.getClass().getClassLoader().getResourceAsStream(path);
System.out.print("2 getClassLoader:");
System.out.println(classLoaderIS);
// (3) 使用classPathResource
ClassPathResource classPathResource = new ClassPathResource(path);
InputStream classPathIS= classPathResource.getInputStream();
System.out.print("3 classPathResource:");
System.out.println(classPathIS);
System.out.println("");
// (二) 读取文件路径
System.out.println("(二) 读取文件路径");
// (1) 读取jar包同级别目录的文件
// 获取jar包所在目录
String jarDir = System.getProperty("user.dir");
String jarPath = jarDir + "/" + path;
System.out.println("1 System.getProperty: " + jarPath);
this.readFile(jarPath);
// !!! 注意下面的方法,在本地可以运行,在jar包中无法访问到数据文件
// (2) 使用ResourceUtils读取工程中的文件
String resourcePath = ResourceUtils.getURL("classpath:").getPath();
System.out.println("2 ResourceUtils: " + resourcePath+path);
this.readFile(resourcePath+path);
// (3) 使用ApplicationHome读取工程中的文件
ApplicationHome home = new ApplicationHome(this.getClass());
String appResourcePath = home.getSource().getPath();
System.out.println("3 ApplicationHome: "+appResourcePath+"/"+path);
this.readFile(appResourcePath+"/"+path);
// (4) 使用getClass读取工程中的文件
String classPath = this.getClass().getResource("/").getPath();
System.out.println("4 getClass: "+classPath+path);
this.readFile(classPath+path);
// (5) 使用getClassLoader读取工程中的文件
String classLoaderPath = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("5 getClassLoader: "+classLoaderPath+path);
this.readFile(classLoaderPath+"/"+path);
return "Hello";
}
public void readFile(String path){
try {
// 按行读文件
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
TestController test = new TestController();
test.readFile();
}
}
3 执行结果
(一) 读取jar包文件中的数据流
1 getClass:java.io.BufferedInputStream@b97c004
2 getClassLoader:java.io.BufferedInputStream@4590c9c3
3 classPathResource:java.io.BufferedInputStream@55f3ddb1
(二) 读取文件路径
1 System.getProperty: D:\1_projects\idea\test/config/data
1111111
2 ResourceUtils: /D:/1_projects/idea/test/target/classes/config/data
1111111
3 ApplicationHome: D:\1_projects\idea\test\target\classes/config/data
1111111
4 getClass: /D:/1_projects/idea/test/target/classes/config/data
1111111
5 getClassLoader: /D:/1_projects/idea/test/target/classes/config/data
1111111