package cn.itcast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Stream4_copy {
public static void main(String[] args) throws IOException {
long Before = System.currentTimeMillis();
// method1(); //22272
// method2(); //73
// method3(); //457
method4(); // 28
long After = System.currentTimeMillis();
System.out.println(After - Before);
}
private static void method1() throws IOException {
// 创建对象
FileInputStream fis = new FileInputStream("dengziqi.mp3");
FileOutputStream fos = new FileOutputStream("a.mp3");
// 写入,写出
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
// 关闭流
fis.close();
fos.close();
}
private static void method2() throws IOException {
// 创建对象
FileInputStream fis = new FileInputStream("dengziqi.mp3");
FileOutputStream fos = new FileOutputStream("a.mp3");
// 写入,写出
int len;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
// 关闭流
fis.close();
fos.close();
}
private static void method3() throws IOException {
// 创建对象
FileInputStream fis = new FileInputStream("dengziqi.mp3");
FileOutputStream fos = new FileOutputStream("a.mp3");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入,写出
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
// 关闭流
fis.close();
fos.close();
}
private static void method4() throws IOException {
// 创建对象
FileInputStream fis = new FileInputStream("dengziqi.mp3");
FileOutputStream fos = new FileOutputStream("a.mp3");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入,写出
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
// 关闭流
bis.close();
bos.close();
}
}
|
|