1 package cn.itcast.p7.io.bytestream.test; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class CopyMp3Test { 10 11 /** 12 * @param args 13 * @throws IOException 14 */ 15 public static void main(String[] args) throws IOException { 16 17 copy_4(); 18 19 } 20 // 千万不要用,效率没有! 21 public static void copy_4() throws IOException { 22 FileInputStream fis = new FileInputStream("c:\\0.mp3"); 23 FileOutputStream fos = new FileOutputStream("c:\\4.mp3"); 24 25 26 int ch = 0; 27 28 while((ch =fis.read())!=-1){ 29 fos.write(ch); 30 } 31 32 fos.close(); 33 fis.close(); 34 } 35 36 //不建议。 37 public static void copy_3() throws IOException { 38 FileInputStream fis = new FileInputStream("c:\\0.mp3"); 39 FileOutputStream fos = new FileOutputStream("c:\\3.mp3"); 40 41 byte[] buf = new byte[fis.available()]; 42 fis.read(buf); 43 fos.write(buf); 44 fos.close(); 45 fis.close(); 46 } 47 48 public static void copy_2() throws IOException { 49 50 FileInputStream fis = new FileInputStream("c:\\0.mp3"); 51 BufferedInputStream bufis = new BufferedInputStream(fis); 52 53 FileOutputStream fos = new FileOutputStream("c:\\2.mp3"); 54 BufferedOutputStream bufos = new BufferedOutputStream(fos); 55 56 57 58 int ch = 0; 59 60 while((ch=bufis.read())!=-1){ 61 bufos.write(ch); 62 } 63 64 bufos.close(); 65 bufis.close(); 66 } 67 68 public static void copy_1() throws IOException { 69 70 FileInputStream fis = new FileInputStream("c:\\0.mp3"); 71 FileOutputStream fos = new FileOutputStream("c:\\1.mp3"); 72 73 byte[] buf = new byte[1024]; 74 75 int len = 0; 76 77 while((len=fis.read(buf))!=-1){ 78 fos.write(buf,0,len); 79 } 80 81 fos.close(); 82 fis.close(); 83 } 84 85 86 87 }
时间: 2024-10-25 02:24:22