赞
踩
/** * 方法一:使用类加载器的getResource().getPath()获取全路径再拼接文件名,最后根据文件路径获取文件流 * 备注:jar包不可用,因为jar包中没有一个实际的路径存放文件 * * @param fileName * @return * @throws FileNotFoundException */ public BufferedReader function1(String fileName) throws FileNotFoundException { // /Users/zunf/code/read-resource/target/classes/ String path = this.getClass().getClassLoader().getResource("").getPath(); // /Users/zunf/code/read-resource/target/classes/测试.txt String filePath = path + fileName; return new BufferedReader(new FileReader(filePath)); }
/** * 方法二:使用类加载器的getResource().getPath(),传参直接获取文件路径,再根据文件路径获取文件流 * 备注:jar包不可用,因为jar包中没有一个实际的路径存放文件 * * @param fileName * @return * @throws IOException */ public BufferedReader function2(String fileName) throws IOException { // /Users/zunf/code/read-resource/target/classes/%e6%b5%8b%e8%af%95.txt String filePath = this.getClass().getClassLoader().getResource(fileName).getPath(); //可以看到上面读取到路径的中文被URLEncoder编码了,所以需要先用URLDecoder解码一下,恢复中文 filePath = URLDecoder.decode(filePath, "UTF-8"); return new BufferedReader(new FileReader(filePath)); }
/** * 方法三:使用类加载器的getResourceAsStream(),直接获取文件流 * 备注:jar包可用 * * @param fileName * @return * @throws IOException */ public BufferedReader function3(String fileName) throws IOException { //getResourceAsStream(fileName) 等价于getResource(fileName).openStream() InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName); if (inputStream == null) { throw new FileNotFoundException(fileName); } return new BufferedReader(new InputStreamReader(inputStream)); }
/** * 方法四:使用Class对象的getResourceAsStream()获取文件流 * 备注:jar包可用 * * @param fileName * @return * @throws IOException */ public BufferedReader function4(String fileName) throws IOException { //getResourceAsStream(fileName) 等价于getResource(fileName).openStream() InputStream inputStream = this.getClass().getResourceAsStream("/" + fileName); if (inputStream == null) { throw new FileNotFoundException(fileName); } return new BufferedReader(new InputStreamReader(inputStream)); }
源码里还是方法三、方法四,只不过做了一些封装,更方便开发
/**
* 方法五:使用Spring提供的ClassPathResource获取
* 备注:jar包可用
*
* @param fileName
* @return
* @throws IOException
*/
public BufferedReader function5(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
return new BufferedReader(new InputStreamReader(inputStream));
}
/**
* 方法六:使用Hutool的ResourceUtil
* 备注:jar包可用
*
* @param fileName
* @return
* @throws IOException
*/
public BufferedReader function6(String fileName) throws IOException {
List<URL> resources = ResourceUtil.getResources(fileName);
URL resource = resources.get(0);
return new BufferedReader(new InputStreamReader(resource.openStream()));
}
1)编写接口
//Jar包启动时根据传入的不同funcation值来选择调用哪个方法测试 @Value("${function}") private int function; @GetMapping("/test") public String test() throws IOException { //需要在resource里读取的文件 String fileName = "测试.txt"; //调用方法 System.out.println("调用function" + function); BufferedReader bufferedReader = null; switch (function) { case 1: bufferedReader = function1(fileName); break; case 2: bufferedReader = function2(fileName); break; case 3: bufferedReader = function3(fileName); break; case 4: bufferedReader = function4(fileName); break; case 5: bufferedReader = function5(fileName); break; case 6: bufferedReader = function6(fileName); break; default: } //读取并输出 StringBuilder sb = new StringBuilder(); String line = null; while ((line = bufferedReader.readLine()) != null) { sb.append(line).append("\n"); } System.out.println(sb); return sb.toString(); }
2)启动jar包指令
java -jar -Dfunction=6 read-resource-1.0-SNAPSHOT.jar
-Dfunction=6
的值就能动态切换方法了。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。