javaSE中的輸入輸出資料流---一個讀取流,對應多個輸出資料流。而且產生的片段檔案都有有序的編號,javase輸出資料流
<span style="font-size:18px;">package com.io.other.split;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class SplitFileTest {/** * 檔案切割器。 * 一個讀取流,對應多個輸出資料流。而且產生的片段檔案都有有序的編號 * @param args * @throws IOException */private static final int DEFAULT_SIZE=1024*2;public static void main(String[] args) throws IOException {File srcFile=new File("chen\\11.avi");File destDir=new File("chen\\partfiles");SplitFile(srcFile,destDir);}/** * * @param srcFile * @throws IOException */public static void SplitFile(File srcFile,File destDir) throws IOException {if (!srcFile.exists()) {throw new RuntimeException(srcFile.getAbsolutePath()+",源檔案不存在");} if(!destDir.exists()){destDir.mkdirs();}//1、定義一個源檔案FileInputStream fis=new FileInputStream(srcFile);//2、建立目的引用FileOutputStream fos=null;//3、建立一個緩衝區儲存資料byte[] buf=new byte[DEFAULT_SIZE];int len=0;int count=0;while((len=fis.read(buf))!=-1){//建立輸出資料流,並明確要寫入的檔案File partFile=new File(destDir,(++count)+".part");fos=new FileOutputStream(partFile);fos.write(buf, 0, len);fos.close();}//在產生片段檔案時,需要產生一個設定檔,記錄片段的個數和源檔案名稱字//partfile=n;filename=11.avi//設定檔中儲存的索引值資訊。使用Properties集合Properties prop=new Properties();prop.setProperty("partcount", Integer.toString(count));prop.setProperty("filename", srcFile.getName());File conFile=new File(destDir,(++count)+".properties");fos=new FileOutputStream(conFile);//關閉資源fos.close();fis.close();}}</span>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。