IO流--切割 合并文件

import java.io.*;
import java.util.*;

public class io {
    public static void main(String[] args)throws IOException
    { 
        splitFile();
        merge();
    }
    
    //切割文件
    public static void splitFile() throws IOException
    {
        FileInputStream fis = new FileInputStream("e:\\4.jpg");
        FileOutputStream fos = null;
        
        byte[] buf = new byte[1024*1024];
        int len = 0;
        int count = 1;
        while ((len=fis.read(buf))!=-1)
        {
            fos = new FileOutputStream("e:\\"+(count++)+".part");
            fos.write(buf,0,len);
            fos.close();
        }
        fis.close();
    }
    
    //合并文件
    public static void merge() throws IOException
    {
        Vector<FileInputStream> v = new Vector<FileInputStream>();
        
        v.add(new FileInputStream("e:\\1.txt"));
        v.add(new FileInputStream("e:\\2.txt"));
        v.add(new FileInputStream("e:\\3.txt"));
        
        Enumeration<FileInputStream> en = v.elements();
        SequenceInputStream sis = new SequenceInputStream(en);
        FileOutputStream fos = new FileOutputStream("e:\\4.txt");
        
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len=sis.read(buf))!=-1) 
        {
            fos.write(buf,0,len);    
        }
        fos.close();
        sis.close();
        
    }

}

 

你可能感兴趣的:(IO流)