Java字节流复制MP3

练习:使用不同的方法复制一个MP3

 

CopyMp3Test.java

import java.io.*;

class CopyMp3Test {

   public static void main(String args[]) throws IOException {
      copy_1();
      copy_2();
      copy_2_2();
      copy_3();
      copy_4();
   }

   // 所耗时间:173
   public static void copy_1() throws IOException {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      FileOutputStream fos = new FileOutputStream("C:\\1.mp3");
      byte b[] = new byte[1024];
      int len = 0;
      long time1 = System.currentTimeMillis();
      while ((len = fis.read(b)) != -1) {
         fos.write(b, 0, len);
      }
      fos.close();
      fis.close();
      long time2 = System.currentTimeMillis();
      long time = time2 - time1;
      System.out.println("所耗时间:" + time);
   }

   // 所耗时间:118
   public static void copy_2() throws IOException {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      BufferedInputStream bufis = new BufferedInputStream(fis);
      FileOutputStream fos = new FileOutputStream("C:\\2.mp3");
      BufferedOutputStream bufos = new BufferedOutputStream(fos);
      byte b[] = new byte[1024];
      int len = 0;
      long time1 = System.currentTimeMillis();
      while ((len = bufis.read(b)) != -1) {
         bufos.write(b, 0, len);
         bufos.flush();
      }
      bufos.close();
      bufis.close();
      long time2 = System.currentTimeMillis();
      long time = time2 - time1;
      System.out.println("所耗时间:" + time);
   }

   // 所耗时间:65926
   public static void copy_2_2() throws IOException {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      BufferedInputStream bufis = new BufferedInputStream(fis);
      FileOutputStream fos = new FileOutputStream("C:\\2_2.mp3");
      BufferedOutputStream bufos = new BufferedOutputStream(fos);
      int ch = 0;
      long time1 = System.currentTimeMillis();
      while ((ch = bufis.read()) != -1) {
         bufos.write(ch);
         bufos.flush();
      }
      bufos.close();
      bufis.close();
      long time2 = System.currentTimeMillis();
      long time = time2 - time1;
      System.out.println("所耗时间:" + time);
   }

   // 所耗时间:35
   public static void copy_3() throws IOException// 200毫秒
   {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      FileOutputStream fos = new FileOutputStream("C:\\3.mp3");
      byte b[] = new byte[fis.available()];
      long time1 = System.currentTimeMillis();
      fis.read(b);
      fos.write(b);
      fos.close();
      fis.close();
      long time2 = System.currentTimeMillis();
      long time = time2 - time1;
      System.out.println("所耗时间:" + time);
   }

   // 所耗时间:129448
   public static void copy_4() throws IOException {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      FileOutputStream fos = new FileOutputStream("C:\\4.mp3");
      int ch = 0;
      long time1 = System.currentTimeMillis();
      while ((ch = fis.read()) != -1) {
         fos.write(ch);
      }
      fos.close();
      fis.close();
      long time2 = System.currentTimeMillis();
      long time = time2 - time1;
      System.out.println("所耗时间:" + time);
   }
}