Flask接口签名sign原理与实例代码浅析
417
2022-12-29
java多线程读取多个文件的方法
本文实例为大家分享了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 fiMjXfDgORleIndex;
private List
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
return filelist;
}
public void setFilelist(List
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.MjXfDgORreadLine()) != 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 IMjXfDgOROException {
String filepath = "D:\\LocalFtpServer\\data20181229\\";
File file = new File(filepath);
//读取目录下所有文件
String[] filelist = file.list();
List
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();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~