zoukankan      html  css  js  c++  java
  • java文件分割和合并

    package search;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class Split {
        
        /**每个小文件大小*/
        static final int SIZE = 1024*1024*5;
        /**需要分割的文件*/
        static final String SOURCE = "E:/t_question.sql";
        /**分割后输出的目录*/
        static final String SOURCE_OUT = "E:/test/";
        /**合并后的文件*/
        static final String DST = "G:/quesion.sql";
    
        public static void main(String[] args) throws IOException{
            split();
        }
        /**
         * 文件分割
         * @throws IOException
         */
        public static void split() throws IOException{
    
            File file = new File(SOURCE);
            FileInputStream raf = new FileInputStream(file);
            FileChannel fc = raf.getChannel();
            long fileSize = fc.size();
            
            int count = (int) (fileSize/SIZE);
            ByteBuffer dst = ByteBuffer.allocate(SIZE);
            for(int i=0 ; i <=count  ; i++){
                FileOutputStream fs = new FileOutputStream(new File(SOURCE_OUT+i));
                if(i!=count){
                    fc.read(dst,SIZE*i);
                    fs.write(dst.array());
                }else{
                    fileSize = fileSize -SIZE*count;
                    System.out.println(fileSize);
                    ByteBuffer dstTail = ByteBuffer.allocate((int) fileSize);
                    fs.write(dstTail.array());
                }
                fs.flush();
                dst.clear();
            }
        
        }
        
        /**
         * 合并文件
         * @throws Exception
         */
        public static void merge()throws Exception{
            File parentFile = new File(SOURCE_OUT);
            File fileDst = new File(DST);
            FileChannel fs =  new FileOutputStream(fileDst, true).getChannel();
            try{
                 for(File file :parentFile.listFiles()){
                     FileChannel fc = new FileInputStream(file).getChannel();
                     ByteBuffer by = ByteBuffer.allocate((int) fc.size());
                     fc.read(by);
                     by.position(0);
                     fs.write(by);
                     
                 }
            }finally{
                fs.close();
            }
        }
    }
  • 相关阅读:
    为什么nhibernate 不能保存ontomany的结构
    如何使用 Mmcv.exe 工具来管理群集消息队列资源[转]
    Building an MSMQ Cluster[转]
    XmlSerializerFactory Is Pretty Cool[转]
    Processing Binary Documents Through BizTalk Via Web Services[转]
    获取数据字典
    Using XPaths in Message Assignment[转]
    MSMQ突破4M限制的方法
    XmlSerializerFactory Is Pretty Cool[转]
    BizTalk自定义证书加密/解密pipeline组件开发
  • 原文地址:https://www.cnblogs.com/JimmyXie/p/3806317.html
Copyright © 2011-2022 走看看