import java.io.*;
public class BufferedTest {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
long start = System.currentTimeMillis();
try {
// 1.实例化File类的对象,指明要操作的文件
File srcFile = new File("飘窗.jpg");
File destFile = new File("飘窗1.jpg");
// 2.1造节点流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
// 2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 3.复制的细节:读取写入
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {// 4.关闭资源
// 要求:先关闭外层的流,再关闭内层的流
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 可以省略(关闭外层流的同时,内层流也会自动的进行关闭)
// if (fos != null) {
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (fis != null) {
// try {
// fis.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
long end = System.currentTimeMillis();
System.out.println("复制花费的时间为:" + (end - start));
}
}