Java的IO操作(四) – SequenceInputStream類,執行個體:一個檔案分割、合并工具

來源:互聯網
上載者:User

SequenceInputStream可以看做是多個InputStream對象的有序集合。當一個InputStream對象的資料讀取完後,它會自動取出下一個InputStream對象進行讀取,直到所有的InputStream對象都讀取完為止。

利用這個特點,我們來編寫一個檔案分割、合并工具。

使用說明:

SequenceDemo [Option] [filePath] [number]

Option:

-c : 表示要合并檔案

-s : 表示要分割檔案

filePath:

指定一個檔案路徑

number:

指定單個檔案大小或檔案數量

如:

java cls.SequeceDemo -s F:\QQ.exe 102400

表示將QQ.exe分割成多個檔案大小為102400位元組的小檔案。

java cls.SequeceDemo -m F:\QQ.exe 13

表示將已經被分割好的13個QQ.exe檔案合并成一個整檔案。

注意,構造SequenceOutputStream對象時需要一個Enumeration對象的引用做為參數。Enumeration是一個介面,我們必須實現這個介面中的hasMoreElements()方法和nextElement()方法。

完整代碼:

package cls;import java.io.*;import java.util.*;public class SequenceStreamDemo{    public static void main(String[] args) throws Exception    {        switch(args[0].charAt(1))        {            case 's': // 分割檔案            {                SequenceStreamDemo.seperate(args[1],Long.parseLong(args[2]));                break;            }            case 'm': // 合并檔案            {                SequenceStreamDemo.merge(args[1],Integer.parseInt(args[2]));                break;            }            default: // 參數輸入錯誤            {                System.out.println("非法參數");                break;            }        }    }        /* 合并檔案    * fileName : 檔案名稱    * amount : 要合并的小檔案的數量    **/    public static void merge(String fileName,int amount) throws Exception    {        // 建立合并後的檔案        File file = new File(fileName);        FileOutputStream fis = new FileOutputStream(file);        BufferedOutputStream bos = new BufferedOutputStream(fis);                // 將多個小檔案對象儲存在List中        List<InputStream> list = new ArrayList<InputStream>();        for(int i = 0 ; i < amount ; ++i)        {            File f = new File(fileName + "_" + i);            list.add(new FileInputStream(f));        }                final Iterator<InputStream> it = list.iterator();                // 構建Enumeration對象        Enumeration<InputStream> em = new Enumeration<InputStream>()        {            public boolean hasMoreElements()            {                return it.hasNext();            }            public InputStream nextElement()            {                return it.next();            }        };                // 使用SequenceInputStream合并檔案        BufferedInputStream bis = new BufferedInputStream(new SequenceInputStream(em),8192);        int data;                while((data = bis.read()) != -1)        {            bos.write(data);        }                bis.close();        bos.flush();        bos.close();                System.out.println("合并完成");    }    /* 分割檔案    * fileName : 檔案名稱    * size : 單個檔案的大小    **/    public static void seperate(String fileName,long size) throws Exception    {        File file = new File(fileName);        long number;                // 計算分割後的檔案數量        if(0 == file.length() % size)        {            number = file.length() / size;        }        else        {            number = file.length() / size + 1;        }                FileInputStream fis = new FileInputStream(file);        BufferedInputStream bis = new BufferedInputStream(fis);                // 開始分割        byte[] buf = new byte[1];        for(int i = 0 ; i < number ; ++i)        {            FileOutputStream fos = new FileOutputStream(new File(fileName + "_" + i));            BufferedOutputStream bos = new BufferedOutputStream(fos);                        long curSize = 0L;            while(bis.read(buf) != -1)            {                bos.write(buf);                                curSize++;                if(curSize == size) // 已經分割成了一個小檔案                {                    bos.flush();                    bos.close();                    break;                }            }            if(curSize < size) // 打包剩下的資料            {                bos.flush();                bos.close();                break;            }                    }                System.out.println("分割完成");    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.