java多线程读取多个文件的方法
作者:戴着红领巾走世界 时间:2022-12-05 04:37:45
本文实例为大家分享了java多线程读取多个文件的具体代码,供大家参考,具体内容如下
工具类代码如下:
import java.io.*;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* 多线程读取多个文件
*/
public class FileThread extends Thread{
private final CountDownLatch countDownLatch = new CountDownLatch(10);
private int fileIndex;
private List<String> filelist;
private String filepath = "D:\\LocalFtpServer\\data20181229\\";
private String movepath = "D:\\LocalFtpServer\\data20181229_01\\";
public int getFileIndex() {
return fileIndex;
}
public void setFileIndex(int fileIndex) {
this.fileIndex = fileIndex;
}
public List<String> getFilelist() {
return filelist;
}
public void setFilelist(List<String> filelist) {
this.filelist = filelist;
}
@Override
public void run() {
for (int i = 0; i < filelist.size(); i++) {
if (i % 10 == fileIndex) {
//读取文件
File readfile = new File(filepath + filelist.get(i));
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(readfile), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String line = null;
// 一次读入一行,直到读入null为文件结束
while ((line = reader.readLine()) != null) {
System.out.println(line );
}
reader.close();
isr.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//读取完后, 移动文件位置
readfile.renameTo(new File(movepath + readfile.getName()));
}
}
countDownLatch.countDown();
}
}
调用测试:
public static void main(String[] args) throws IOException {
String filepath = "D:\\LocalFtpServer\\data20181229\\";
File file = new File(filepath);
//读取目录下所有文件
String[] filelist = file.list();
List<String> fList=new ArrayList<String>();
for (int i = 0; i < filelist.length; i++) {
if (filelist[i].startsWith("data") && filelist[i].endsWith(".txt")) {
fList.add(filelist[i]);
}
}
for(int i=0;i<30;i++){
FileThread fileThread=new FileThread();
fileThread.setFileIndex(i);
fileThread.setFilelist(fList);
fileThread.start();
}
countDownLatch.await();
}
来源:https://blog.csdn.net/HoneyYHQ9988/article/details/85601628
标签:java,多线程,读取文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 快速实现防止网络重复请求&按钮重复点击的方法
2023-08-29 17:31:45
Android基于ViewPager实现的应用欢迎界面完整实例
2021-10-02 23:01:52
Springboot使用@RefreshScope注解实现配置文件的动态加载
2022-06-11 10:06:00
![](https://img.aspxhome.com/file/2023/9/69909_0s.jpg)
Java 线程池ThreadPoolExecutor源码解析
2022-06-28 09:13:59
![](https://img.aspxhome.com/file/2023/7/64767_0s.png)
java简单实现用语音读txt文档方法总结
2022-11-10 22:02:54
![](https://img.aspxhome.com/file/2023/1/70491_0s.png)
C# 获取当前年份的周期及周期所在日期范围(推荐)
2021-10-06 15:00:44
![](https://img.aspxhome.com/file/2023/1/66131_0s.png)
关于mybatis resulttype 返回值异常的问题
2021-08-09 20:26:19
Java设计模式之建造者模式
2023-09-03 14:40:37
Springboot引入多个yml方法(多种方案)
2023-07-21 21:10:48
![](https://img.aspxhome.com/file/2023/7/88597_0s.png)
Java IO流和文件操作实现过程解析
2022-03-10 02:08:13
详细解读java同步之synchronized解析
2022-08-01 15:27:05
C#之WinForm跨线程访问控件实例
2023-11-20 19:05:13
c#动态类型,及动态对象的创建,合并2个对象,map实例
2023-04-28 17:40:12
Java爬取豆瓣电影数据的方法详解
2021-12-12 16:21:06
![](https://img.aspxhome.com/file/2023/1/79871_0s.png)
C#实现异步GET的方法
2022-08-17 11:52:55
C#中使用快速排序按文件创建时间将文件排序的源码
2023-06-04 08:19:19
spring boot如何加入mail邮件支持
2021-09-27 15:12:57
浅谈Java 继承接口同名函数问题
2023-07-22 13:28:47
![](https://img.aspxhome.com/file/2023/0/57720_0s.jpg)
Android列表点击事件定义的一些思考
2021-12-24 08:41:40
![](https://img.aspxhome.com/file/2023/9/137509_0s.jpg)
Minio与SpringBoot使用okhttp3问题解决
2021-06-25 19:17:08
![](https://img.aspxhome.com/file/2023/1/83501_0s.jpg)