本人针对400MB得视频测试复制速度,结果发现RandomAccessFile复制文件相比BufferedOutputStream花了少一半得时间
有图有真相:
下面是实现代码:
- package com.wangzhe;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- public class Test {
- public static void main(String[] args) throws IOException {
-
- long start,stop;
- start =System.currentTimeMillis();
- test1();
- stop =System.currentTimeMillis();
- System.out.println((stop-start)+"毫秒");//4854毫秒
-
- start =System.currentTimeMillis();
- test2();
- stop =System.currentTimeMillis();
- System.out.println((stop-start)+"毫秒");//7169毫秒
- }
- public static void test2() throws IOException {
- BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\1.avi"));
- BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\\3.avi"));
- byte[] bytes=new byte[8192];
- int len=0;
- while((len=bis.read(bytes))!=-1){
- bos.write(bytes);
- bos.flush();
- }
- bis.close();
- bos.close();
-
- }
- public static void test1() throws IOException {
-
- RandomAccessFile readFile=new RandomAccessFile("D:\\1.avi","r");
- RandomAccessFile writeFile=new RandomAccessFile("E:\\2.avi","rw");
- byte[] bytes=new byte[8192];
- int len=0;
- while((len=readFile.read(bytes))!=-1){
- writeFile.write(bytes);
- }
- readFile.close();
- writeFile.close();
-
- }
- }
复制代码
|
|