当前位置:网站首页>Read all files under the folder in the jar package
Read all files under the folder in the jar package
2022-06-22 08:39:00 【Alex_ 81D】
The first thing to ask is :
When the file is in resource In the middle of the day , If you read directly locally , Then you can read it , When it comes to success jar After package , No path , This problem is summarized as follows :
We all know springboot The default method is to package into jar Package execution , At this time, if you need to traverse all folders and files under a certain path , Our usual way is to use File To deal with it , But this is because jar package , So use File Class will have an error that the path cannot be found , This article introduces a way to read all the contents of a folder
public static void main(String[] args) throws IOException {
// TODO Load current project classpath Next META-INF/folder And all files in its subfolders
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
// TODO Load current project classpath Next META-INF/folder And all in its subfolders .txt Final document
Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
// TODO Load the current project and all jar in classpath All under META-INF/spring.factories file (springboot The main functions of automatic assembly )
Resource[] resources3 = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "META-INF/spring.factories");
// Traverse the contents of the file
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" Change this path to your own path
// Because I need an array of output streams , It is amended as follows
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;
}The following dependencies should be added to this ( Some automatically ignore )
<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>That is to say springboot Regular... Should not be used in File To read the file , You can only use streaming to read ,
By the way, the traditional way :
private String gossipPath = this.getClass().getResource("/gossip.txt").getPath(); // This kind of local can
InputStream is = this.getClass().getResourceAsStream("/conf/context.xml") // This kind of jar You can also pack , Direct stream read , But what you read is a file , To read all the files in the folder , The above method , It's the best choice
边栏推荐
- luogu P4292 [WC2010]重建计划
- Basic concepts of homomorphic encryption
- How to troubleshoot OOM
- Interpreting the technology group in maker Education
- 一文搞懂one-hot和embedding
- Alibaba cloud ~ simply send SMS
- Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security
- Matlab内数据及数据类型转换
- How to select the appropriate partition key, routing rule and partition number
- I spring and autumn web Penetration Test Engineer (elementary) learning notes (Chapter 2)
猜你喜欢

Thread. Source code analysis of start() method

15 command mode

Web knowledge 4 (filter+listener)

复杂科学在创客教学研究中的应用

The necessity of steam education culture inheritance

兔小巢使用记录

13 代理模式

Mysql+orcle (SQL implements recursive query of all data of child nodes)

Carry out effective maker education courses and activities

Nisp online simulation question bank
随机推荐
steam教育文化传承的必要性
Detailed sorting of Oracle and MySQL pages
Some mathematical operation functions in LabVIEW
Web Knowledge 2 (request+response)
依图在实时音视频中语音处理的挑战丨RTC Dev Meetup
luogu P4292 [WC2010]重建计划
Thread.start()方法源码分析
Fastcorrect: speech recognition fast error correction model RTC dev Meetup
读取jar包里面文件夹下的所有文件
Give priority to static member classes
Golang 开发 常用的第三方库 没有最全只有更全
Flask blog practice - realize the classified management of blogs
Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security
面试突击59:一个表中可以有多个自增列吗?
20 状态模式
矩阵分解
Develop steam education based on scientific skills
Three characteristics of concurrency 2-orderliness
Mysql+orcle (SQL implements recursive query of all data of child nodes)
Chapter II exercise | MNIST dataset | Titanic dataset | image enhancement