一、利用字节流实现媒体文件的拷贝:
public static void copyMedia() throws IOException {
long l1 = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("d:\\往南.mp3");
FileOutputStream fos = new FileOutputStream("g:\\往南.mp3");
byte[] buf = new byte[fis.available()];
fis.read(buf);
fos.write(buf);
fos.close();
fis.close();
ong l2 = System.currentTimeMillis();
System.out.println(l2 - l1);
}
结果:33
二、利用缓冲区提高效率
public static void copy2() throws IOException {
long l1 = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("d:\\往南.mp3");
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("g:\\往南1.mp3");
BufferedOutputStream bufos = new BufferedOutputStream(fos);
byte[] buf = new byte[bufis.available()];
bufis.read(buf);
bufos.write(buf);
bufos.close();
bufis.close();
long l2 = System.currentTimeMillis();
System.out.println(l2 - l1);
}
结果:10