带缓冲区的字节流

 1 package com.itheima;
 2 import java.io.BufferedInputStream;
 3 import java.io.BufferedOutputStream;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 public class Test06 {
 8 
 9     /**
10      * 需求:使用带缓冲功能的字节流复制文件。
11      */
12     public static void main(String[] args) {
13         // TODO Auto-generated method stub
14         long start = System.currentTimeMillis();
15         copy();
16         long end = System.currentTimeMillis();
17         System.out.println((end-start)+"毫秒");//查看拷贝用时
18     }
19     //通过字节流的缓冲区复制图片
20     public static void copy() {
21         
22         FileInputStream fis = null;
23         FileOutputStream fos = null;
24         BufferedInputStream bufis = null;
25         BufferedOutputStream bufos = null;
26         try
27         {
28             fis = new FileInputStream("c:\\1.jpg");
29             fos = new FileOutputStream("c:\\copy_1.jpg");
30             bufis = new BufferedInputStream(fis);
31             bufos = new BufferedOutputStream(fos);
32             int by = 0;
33             while ((by=bufis.read())!=-1)
34             {
35                 bufos.write(by);
36             }
37         }
38         catch (IOException e)
39         {
40             throw new RuntimeException("读写失败");
41         }
42         finally
43         {
44             try
45             {
46                 bufis.close();
47                 bufos.close();
48             }
49             catch (IOException e)
50             {
51                 throw new RuntimeException("关闭异常");
52             }
53                 
54         }
55     }
56 }

 

posted on 2014-01-21 02:15  GoBackHome  阅读(698)  评论(0编辑  收藏  举报

导航