文件拆分、合并

 

import java.io.*;


public class IOTest {
     public static void main(String[] args){
         //拆分文件
         devide();
         //合并文件
         merge();
     }
     public static void devide(){
         //要被拆分的目标文件
         File file = new File("E:\\merge\\sxcs项目执行流程.txt");
         //文件读写流
         FileInputStream fileInputStream = null;
         FileOutputStream fileOutputStream = null;
         int num = 0;
         try {
            //获取文件的输入流,用来读取文件
            fileInputStream = new FileInputStream(file);
            //每次读的字节大小为100k
            byte[] by = new byte[100];
            while((fileInputStream.read(by)) != -1){
                //新建存放拆分后字节的文件
                File files = new File("E:\\merge\\sxcs项目执行流程"+num+".txt");
                //获取输入流
                fileOutputStream = new FileOutputStream(files);
                //写入文件
                fileOutputStream.write(by);
                num++;
                //重新初始化by数组,清空内容,如果拆分后源文件不是100的整数倍, 写入文件后会将上一个子文件的一部分写进去
                by = new byte[100];
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
                try {
                    if(fileOutputStream != null){
                       fileOutputStream.close();
                    }
                    if(fileInputStream != null){
                        fileInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
     }
     public static void merge(){
         //要合并的文件所在目录, 用来获取需要合并的文件集合
         File file = new File("E:\\merge");
         //合并后文件存放的位置
         File outFile = new File("E:\\merge\\merge.txt");
         //获取需要合并的文件集合
         File[] filelistFiles = file.listFiles();
         FileInputStream fileInputStream = null;
         FileOutputStream fileOutputStream = null;
         //循环每一个文件, 合并到一个文件重
         for(File f : filelistFiles){
             try {
                 //存储字节大小为文件大小
                 byte[] by = new byte[(int)f.length()];
                 fileInputStream = new FileInputStream(f);
                 fileInputStream.read(by);
                 //将读取出来的文件,采用追加的方式添加到文件中
                 fileOutputStream = new FileOutputStream(outFile,true);
                 fileOutputStream.write(by);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                    try {
                        if(fileInputStream != null){
                           fileInputStream.close();
                        }
                        if(fileOutputStream != null){
                            fileOutputStream.close();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
            
         }
     }
}