本帖最后由 Up↑Lee↗ 于 2014-4-5 08:32 编辑
方式2自定义字节流的缓冲区为什么比方式1BufferedInputStream 速度快??
- /*
- 演示mp3的复制。通过缓冲区。
- BufferedOutputStream
- BufferedInputStream
- */
- import java.io.*;
- class CopyMp3
- {
- public static void main(String[] args) throws IOException
- {
- long start = System.currentTimeMillis();
- <font color="#ff0000"> copy_2();</font>
- long end = System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- public static void copy_2()throws IOException
- {
- MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("C:\\彩虹.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("C:\\彩虹2.mp3"));
-
- int by = 0;
- //System.out.println("第一个字节:"+bufis.myRead());
- while((by=bufis.myRead())!=-1)
- {
- bufos.write(by); //read方法在提升,而write方法在强转。写入指定字节
- }
- bufos.close();
- bufis.myClose();
- }
- //通过字节流的缓冲区完成复制。
- public static void copy_1()throws IOException
- {
- BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("C:\\彩虹.mp3"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("C:\\彩虹3.mp3"));
-
- int by = 0;
- while((by=bufis.read())!=-1)
- {
- bufos.write(by);
- }
- bufos.close();
- bufis.close();
-
- }
- }
复制代码
|
|