本帖最后由 as604049322 于 2014-12-3 10:39 编辑
- import java.io.*;
- class CopyMp3
- {
- public static void main(String[] args) throws IOException
- {
- long start = System.currentTimeMillis();
- copy_2();
- long end = System.currentTimeMillis();
- System.out.println("copy2消耗时间"+(end-start)+"毫秒");
- }
-
- public static void copy_3()throws IOException
- {
- FileInputStream bufis = new FileInputStream("d:\\in.mp3");
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out3.mp3"));
-
- int num = 0;
- byte[] buf = new byte[1024*4];
- //System.out.println("第一个字节:"+bufis.myRead());
- while((num=bufis.read(buf))!=-1)
- {
- for(byte i:buf)
- bufos.write(i);
- //for(int i=0;i<num;i++)
- //bufos.write(buf[i]);
- }
- bufos.close();
- bufis.close();
- }
- public static void copy_2()throws IOException
- {
- MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("d:\\in.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out2.mp3"));
-
- int by = 0;
- //System.out.println("第一个字节:"+bufis.myRead());
- while((by=bufis.myRead())!=-1)
- {
- bufos.write(by);
- }
- bufos.close();
- bufis.myClose();
- }
- //通过字节流的缓冲区完成复制。
- public static void copy_1()throws IOException
- {
- BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("d:\\in.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out1.mp3"));
-
- int by = 0;
- while((by=bufis.read())!=-1)
- {
- bufos.write(by);
- }
- bufos.close();
- bufis.close();
-
- }
- }
复制代码
昨天看到IO部分,就想我不使用缓冲区,直接写出去能怎么样,然后就自己加上了函数copy_3,然后测试了一下copy_2与copy_3的耗时,明显平均速度还是毕老师使用缓冲区技术快了几毫秒。
不明白具体快在了哪里。。。
|
|