將分割的文件進行合併

將分割後的文件進行合併,還原成一個完整的文件

本質其實就是文件的拷貝,只不過在拷貝的時候是追加文件而不是新建文件

這個時候我們使用到的是字節流,就是將分割後的文件輸入到程序,然後輸出到文件

也可以理解為多個輸入流一個輸出流

<code>package cn.jd.io;

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.OutputStream;

public class Copy {
\tpublic static void main(String[] args) {
\t\tlong t1=System.currentTimeMillis();
\t\tcopy("如何實現手機和電腦的藍牙傳輸.mp4","如何實現手機和電腦的藍牙傳輸2.mp4");
\t\tlong t2=System.currentTimeMillis();//好爽就是這麼塊
\t\tSystem.out.println(t2-t1);
\t}

\tpublic static void copy(String srcPath, String destPath) {
\t\t// 確定源
\t\t//現在是沒有使用緩衝流
\t\tFile src = new File(srcPath);// 讀取的文件,使用輸入流 因為是從文件到程序
\t\tFile src1 = new File(destPath);// 寫入文件,輸出流,從程序到文件
\t\t// 確定流
\t\t//這裡我是對字節流的釋放用了另一種方法

\t\ttry(InputStream is=new BufferedInputStream(new FileInputStream(src));
\t\t\t\tOutputStream os=new BufferedOutputStream(new FileOutputStream(src1));) {
\t\t\t
\t\t\t
\t\t\tbyte[] flush = new byte[3];
//\t\t\t\tSystem.out.println(flush.toString()); 這裡我們只是定義了,裡面還沒有內容
\t\t\tint len = -1;
\t\t\t// 確定方法
\t\t\twhile ((len = is.read(flush)) != -1) { // 通過is.read讀取字節數組的長度
//\t\t\t\t\tSystem.out.println(len);
//\t\t\t\t\tSystem.out.println(flush.toString()); //這裡我不知道怎麼輸出字符了
\t\t\t\tos.write(flush, 0, len); // 將flush裡面的字節寫入到文件 ,這裡要輸入實際的長度
\t\t\t}
\t\t\tos.flush(); // 刷新緩衝流

\t\t} catch (FileNotFoundException e) {

\t\t\te.printStackTrace();
\t\t} catch (IOException e) {
\t\t\t// TODO Auto-generated catch block
\t\t\te.printStackTrace();
\t\t}
\t\t

\t}
}
/<code>



分享到:


相關文章: