多线程拆分大文件,并计算耗时

public class Test {

    public static void main(String[] args) {
        String srcPath = "c:\\test\\main.txt";
        splitFile(srcPath);
    }

    public static void splitFile(String srcPath) {
        BufferedReader bufferedReader = null;
        try {
            File file = new File(srcPath);
            bufferedReader = new BufferedReader(new FileReader(file));
            String line = null;
            int lineNum = 0;
            String symbolContent = null;
            while ((line = bufferedReader.readLine()) != null) {
                lineNum++;
                if (lineNum == 1) {
                    symbolContent = line;
                }
            }
            System.out.println("第一行数据:" + symbolContent);
            int total = Integer.valueOf(symbolContent.substring(11));
            System.out.println("记录总数:" + total);
            int threadNum = 10;
            //计算每个线程处理记录数
            int singleProcess = 0;
            if (total % threadNum == 0) {
                singleProcess = total / threadNum;
            } else {
                singleProcess = (total / threadNum) + (total % threadNum);
            }
            System.out.println("每个线程处理记录数:" + singleProcess);
            CountDownLatch latch = new CountDownLatch(threadNum);
            long start = System.currentTimeMillis();
            for (int i = 0; i < threadNum; i++) {
                int startIndex = i * singleProcess;
                MultiThreadReadFile multiThreadReadFile = new MultiThreadReadFile(file, startIndex, singleProcess, latch);
                Thread thread = new Thread(multiThreadReadFile);
                thread.start();
            }
            latch.await();
            long end = System.currentTimeMillis();
            System.out.println("拆分文件耗时:" + (end - start) + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class MultiThreadReadFile implements Runnable {

    private File file;
    private int startIndex;
    private int size;
    private CountDownLatch latch;

    public MultiThreadReadFile(File file, int startIndex, int size, CountDownLatch latch) {
        this.file = file;
        this.startIndex = startIndex;
        this.size = size;
        this.latch = latch;
    }

    @Override
    public void run() {
        readFile(file, startIndex, size);
    }

    public void readFile(File file, int startIndex, int size) {
        String outFilePath = "c:\\test\\dir.txt";
        File outFile = new File(outFilePath);
        LineNumberReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new LineNumberReader(new FileReader(file));
            writer = new BufferedWriter(new FileWriter(outFile));
            int endIndex = startIndex + size;
            String line = null;
            int currentNum = 0;
            while ((line = reader.readLine()) != null && (currentNum = reader.getLineNumber()) < (endIndex + 2)) {
                if (currentNum != 1 && currentNum >= startIndex + 2) {
                    writer.write(line + "\r");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            latch.countDown();
        }
    }
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值