当前位置:网站首页>读取jar包里面文件夹下的所有文件
读取jar包里面文件夹下的所有文件
2022-06-22 08:18:00 【Alex_81D】
这块先抛出问题所在:
当文件在resource中时,如果在本地直接读,那么是可以读到的,当打成jar包后,就得不到路径,对这个问题做如下总结:
我们都知道springboot默认的方式是打包成jar包执行的,这个时候如果需要遍历某个路径下的所有文件夹和文件,按照我们常规的方式是使用File来处理,但是这里由于是jar包,所以使用File类会出现找不到路径的错误,本文介绍一种可以读取文件夹下所有内容的方式
public static void main(String[] args) throws IOException {
// TODO 加载当前项目classpath下META-INF/folder及其子文件夹中的所有文件
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
// TODO 加载当前项目classpath下META-INF/folder及其子文件夹中的所有以.txt结尾的文件
Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
// TODO 加载当前项目及所有jar中classpath下的所有META-INF/spring.factories文件(springboot自动装配的主要功能)
Resource[] resources3 = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "META-INF/spring.factories");
// 遍历文件内容
for(Resource resource : resources) {
StringBuffer script = new StringBuffer();
try(InputStreamReader isr = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
BufferedReader bufferReader = new BufferedReader(isr)) {
String tempString;
while ((tempString = bufferReader.readLine()) != null) {
script.append(tempString).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("script:" + script.toString());
}
}"META-INF/folder/**/*.txt" 这个路径换成自己的路径即可
//因为我需要输出流数组,修改为如下
public InputStream[] getDirResource() throws IOException
{
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "/Corpus/Chinese/*.xml");
Resource[] resourcesPi = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "/Corpus/pi/*.xml");
int lengths = resources.length + resourcesPi.length;
InputStream[] files = new InputStream[lengths];
for (int i = 0, n = resources.length; i < n; i++)
files[i] =resources[i].getInputStream();
int count = 0;
for (int i = resources.length, n = lengths; i < n; i++){
files[i] =resourcesPi[count].getInputStream();
count++;
}
return files;
}这块需要加如下依赖(有的自动忽略)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>即在springboot中不应该使用常规的File来读取文件了,只能使用流的方式来读取,
顺便说一下传统的方式:
private String gossipPath = this.getClass().getResource("/gossip.txt").getPath(); //这种本地可以
InputStream is = this.getClass().getResourceAsStream("/conf/context.xml") //这种有jar包时也可以,直接流读取,但是读取的是文件,要想读取文件夹里面的所有文件,那上面的方法,是不二之选
边栏推荐
- Optimization of MySQL paging scheme by database and table
- Example of multipoint alarm clock
- FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
- The necessity of steam education culture inheritance
- Object to string pit
- 2022年CIO面临的七大挑战及应对方法
- [Oracle database] wet nurse tutorial day15 DDL, DML, index, view, sequence and deadlock are enough
- EURUSD,H1: invalid lots amount for OrderSend function
- How to create an index
- 解读创客教育中的技术一族
猜你喜欢

Weekly recommended short video: what is the "computing world"?

QT custom composite control (class promotion function)

Interpreting the technology group in maker Education

Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security

The necessity of steam education culture inheritance

Detailed explanation of the underlying principle of concurrent thread pool and source code analysis

QT 自定义组合控件(类提升功能)

Qt 错误提示1: invalid use of incomplete type ‘***‘

EURUSD,H1: invalid lots amount for OrderSend function

Interview shock 59: can there be multiple auto increment columns in a table?
随机推荐
解析认知理论对创客教师实训的作用
.net core 技术栈 网站收集
Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
swagger中的枚举、自定义类型和swaggerignore
中断中为何不能使用信号量,中断上下文为何不能睡眠
Some problems encountered in using swagger in golang
【 Oracle database】 Nursery Mother Tutorial day13 date Function
How to design the dead shot, the best and eye-catching performance of the watch Vanguard
复杂科学在创客教学研究中的应用
C # interface holding structure causes packing problem
Three concurrent features 1- visibility
Square array cyclic right shift
MySQL avoids the method of repeatedly inserting records (ignore, replace, on duplicate key update)
QT custom composite control (class promotion function)
FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
Example of QT qtableview
Some mathematical operation functions in LabVIEW
QT 控件增加双击事件
Example of QT combox
2022年CIO面临的七大挑战及应对方法