zoukankan      html  css  js  c++  java
  • 大日志文件拆分

    package test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class splitFile {
        public static void splitFile(String filePath, int fileCount) throws IOException {
            FileInputStream fis = new FileInputStream(filePath);
            FileChannel inputChannel = fis.getChannel();
            final long fileSize = inputChannel.size();
            long average = fileSize / fileCount;//平均值
            long bufferSize = 200; //缓存块大小,自行调整
            ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.valueOf(bufferSize + "")); // 申请一个缓存区
            long startPosition = 0; //子文件开始位置
            long endPosition = average < bufferSize ? 0 : average - bufferSize;//子文件结束位置
            for (int i = 0; i < fileCount; i++) {
                if (i + 1 != fileCount) {
                    int read = inputChannel.read(byteBuffer, endPosition);// 读取数据
                    readW:
                    while (read != -1) {
                        byteBuffer.flip();//切换读模式
                        byte[] array = byteBuffer.array();
                        for (int j = 0; j < array.length; j++) {
                            byte b = array[j];
                            if (b == 10 || b == 13) { //判断
    
    
                                endPosition += j;
                                break readW;
                            }
                        }
                        endPosition += bufferSize;
                        byteBuffer.clear(); //重置缓存块指针
                        read = inputChannel.read(byteBuffer, endPosition);
                    }
                }else{
                    endPosition = fileSize; //最后一个文件直接指向文件末尾
                }
    
                FileOutputStream fos = new FileOutputStream(filePath + (i + 1));
                FileChannel outputChannel = fos.getChannel();
                inputChannel.transferTo(startPosition, endPosition - startPosition, outputChannel);//通道传输文件数据
                outputChannel.close();
                fos.close();
                startPosition = endPosition + 1;
                endPosition += average;
            }
            inputChannel.close();
            fis.close();
    
        }
    
        public static void main(String[] args) throws Exception {
            splitFile("D:\catalina.out", 5);
        }
    }
  • 相关阅读:
    clear:both其实是有瑕疵的
    CSS3不遥远,几个特性你要知道
    JavaScript使用数组拼接字符串性能如何?
    CSS网页宽度怎么定比较合适
    浅析JavaScript的垃圾回收机制
    淡入淡出效果的js原生实现
    非阻塞式JavaScript脚本及延伸知识
    HTML5 Canvas圣诞树
    Ubuntu查看和自动挂载硬盘
    正则表达式批量重命名
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/14042284.html
Copyright © 2011-2022 走看看