黑馬程式員——java——檔案的切割與合并

來源:互聯網
上載者:User

標籤:位元組流   黑馬程式員   java基礎   文檔   


檔案的切割與合并


import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.Vector;public class FileSplieDemos {public static void main(String[] args) throws Exception {File theReadPath = new File("C:\\Users\\Administrator\\Desktop\\測試檔案夾\\鳳舞九天.mp3");String  theOutPath ="C:\\Users\\Administrator\\Desktop\\測試檔案夾\\abc\\";// 調用自訂方法將一個圖片檔案進行切割SplieFile(theReadPath,theOutPath,new File(".mp3"));//調用自訂方法將這些被分割的檔案合并起String theMergerpathout = "C:\\Users\\Administrator\\Desktop\\測試檔案夾\\abCOPY\\";String themergerInputpath="C:\\Users\\Administrator\\Desktop\\測試檔案夾\\abc\\";String theoutformat = "MergerCopy.mp3";MergerFile(themergerInputpath,theMergerpathout,theoutformat);}private static void MergerFile(String themergerInputpath,String theMergerpathout, String theoutformat) throws Exception {// TODO Auto-generated method stubFile fil = new File(theMergerpathout);if(!(fil.isDirectory())){fil.mkdirs();}//將檔案來源封裝對象File file = new File(themergerInputpath);//擷取所有塊片的檔案File[] names = file.listFiles();//建立一個容器,將所有的檔案名稱字儲存Vector<FileInputStream> v = new Vector<FileInputStream>();for(int x=0;x<names.length;x++){v.add(new FileInputStream(names[x]));}Enumeration<FileInputStream> en = v.elements();//合并多個流對象SequenceInputStream sis = new SequenceInputStream(en);//建立輸出資料流對象BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(theMergerpathout+theoutformat));int len = 0;byte[] b = new byte[1024*1024];while((len=sis.read(b))!=-1){bos.write(b);bos.flush();}}private static void SplieFile(File file,String thepath,File format){// 通過位元組流並聯需要切割的檔案對象,並加入緩衝技術提高效率System.out.println("切割檔案功能啟動");File fil = new File(thepath);if(!(fil.isDirectory())){fil.mkdirs();}BufferedInputStream bis = null;BufferedOutputStream bos = null;int number = 0;try {bis = new BufferedInputStream(new FileInputStream(file));//初使化輸出路徑byte[] b = new byte[1024*1024];    int len  = 0;    while((len = bis.read(b))!=-1)    {    number++;    bos = new BufferedOutputStream(new FileOutputStream(thepath+number+format));    bos.write(b);    bos.flush();        }} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}















黑馬程式員——java——檔案的切割與合并

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.