分割的文件進行合并的代碼
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 {
?? ?public static void main(String[] args) {
?? ??? ?long t1=System.currentTimeMillis();? ?
?? ??? ?copy("如何實現(xiàn)手機和電腦的藍牙傳輸.mp4","如何實現(xiàn)手機和電腦的藍牙傳輸2.mp4");
?? ??? ?long t2=System.currentTimeMillis();//好爽就是這么塊
?? ??? ?System.out.println(t2-t1);
?? ?}
?? ?public static void copy(String srcPath, String destPath) {
?? ??? ?// 確定源
?? ??? ?//現(xiàn)在是沒有使用緩沖流
?? ??? ?File src = new File(srcPath);// 讀取的文件,使用輸入流 因為是從文件到程序
?? ??? ?File src1 = new File(destPath);// 寫入文件,輸出流,從程序到文件
?? ??? ?// 確定流
?? ??? ?//這里我是對字節(jié)流的釋放用了另一種方法
?? ??? ?try(InputStream? is=new BufferedInputStream(new FileInputStream(src));
?? ??? ??? ??? ?OutputStream? os=new BufferedOutputStream(new FileOutputStream(src1));) {
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?byte[] flush = new byte[3];
//?? ??? ??? ??? ?System.out.println(flush.toString());???? 這里我們只是定義了,里面還沒有內(nèi)容
?? ??? ??? ?int len = -1;
?? ??? ??? ?// 確定方法
?? ??? ??? ?while ((len = is.read(flush)) != -1) { // 通過is.read讀取字節(jié)數(shù)組的長度
//?? ??? ??? ??? ??? ?System.out.println(len);
//?? ??? ??? ??? ??? ?System.out.println(flush.toString());?? //這里我不知道怎么輸出字符了
?? ??? ??? ??? ?os.write(flush, 0, len); // 將flush里面的字節(jié)寫入到文件 ,這里要輸入實際的長度
?? ??? ??? ?}
?? ??? ??? ?os.flush(); // 刷新緩沖流
?? ??? ?} catch (FileNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?
?? ?}
}
標(biāo)簽: