您的位置:首页 > 职场人生

黑马程序员_Java实现文件的切割和合并文件

2015-10-07 16:49 645 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

package Demo;

import java.io.*;

import java.util.*;

public class SplitFile {

    /**

     * 文件的切割和合并文件

     * 枚举的使用方法

     * arraylist和vector的区别

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        

        //splitFile();

        combineFile1();

    }

    

    public static void combineFile1()

    {

        //这种方法的效率比较高

        ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();

        FileOutputStream fos = null;

        SequenceInputStream sis = null;

        

        try {

            

            for (int i=1; i<5; i++)

            {

                al.add(new FileInputStream("F:\\zp\\"+i+".part"));

            }

            

            final Iterator it = al.iterator();

            

            Enumeration en = new Enumeration() {

                @Override

                public boolean hasMoreElements() {

                    // TODO Auto-generated method stub

                    return it.hasNext();

                }

                @Override

                public FileInputStream nextElement() {

                    // TODO Auto-generated method stub

                    return (FileInputStream) it.next();

                }

            };

            

            sis = new SequenceInputStream(en);

            fos = new FileOutputStream("F:\\zp\\3.jpg");

            

            byte []buff = new byte[1024];

            int len = 0;

            

            while ((len=sis.read(buff)) != -1)

            {

                fos.write(buff,0,len);

            }

            

        } catch (Exception e) {

            // TODO: handle exception

            e.printStackTrace();

        }

        finally

        {

            try {

                sis.close();

                fos.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

    

    public static void combineFile()

    {

        //安全性比较高

        Vector<FileInputStream> v = new Vector<FileInputStream>();

        FileOutputStream fos = null;

        SequenceInputStream sis=null;

        try {

            

            v.add(new FileInputStream("F:\\zp\\1.part"));

            v.add(new FileInputStream("F:\\zp\\2.part"));

            v.add(new FileInputStream("F:\\zp\\3.part"));

            v.add(new FileInputStream("F:\\zp\\4.part"));

            

            Enumeration e = v.elements();

            sis = new SequenceInputStream(e);

            

            fos = new FileOutputStream("F:\\zp\\2.jpg");

            

            byte []buff = new byte[1024];

            int len = 0;

            

            while ((len=sis.read(buff)) != -1)

            {

                fos.write(buff,0,len);

            }

            

        } catch (Exception e) {

            // TODO: handle exception

            e.printStackTrace();

        }

        finally

        {

            try {

                sis.close();

                fos.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

    

    //对文件进行分割

     public static void splitFile()

    {

         FileOutputStream fos=null;

         FileInputStream fis = null;

         try {

            

             fis = new FileInputStream("F:\\zp\\1.jpg");

            

             //分割的每一小块都是2M,不建议分割大的文件,会导致内存溢出

             byte []buff = new byte[1024*1024*2];

            

             int len = 0;

             int count = 1;

             while ((len=fis.read(buff)) != -1)

             {

                 fos = new FileOutputStream("F:\\zp\\"+(count++)+".part");

                 fos.write(buff,0,len);

                 fos.close();

             }            

            

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

         finally{

             try {

                fis.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

         }        

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 操作 合并