码迷,mamicode.com
首页 > 其他好文 > 详细

IO流--切割 合并文件

时间:2014-07-27 10:21:32      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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流--切割 合并文件

标签:

原文地址:http://www.cnblogs.com/wzz1020/p/3870593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!