利用缓冲区输入输出流完成文件的复制

这里主要是做一个简单的练习:

  1 package com.hw.file0221;
  2 
  3 import java.awt.geom.CubicCurve2D;
  4 import java.io.BufferedInputStream;
  5 import java.io.BufferedOutputStream;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 
 11 public class TestBuffer {
 12     public static void main(String[] args) {
 13         long start1,start2,end1,end2;
 14         
 15         start1 = System.currentTimeMillis();
 16         copyByByte("F://骚操作//testImg.jpg","F://骚操作//copy1.jpg");
 17         end1 = System.currentTimeMillis();
 18         System.out.println("按字节读取:"+(end1-start1));
 19         
 20         start2 = System.currentTimeMillis();
 21         copyByByteArray("F://骚操作//testImg.jpg","F://骚操作//copy2.jpg");
 22         end2 = System.currentTimeMillis();
 23         System.out.println("按字节数组读取:"+(end2-start2));
 24     }
 25     
 26     public static void copyByByte(String sourceName,String targetName){
 27         BufferedInputStream input = null;
 28         BufferedOutputStream output = null;
 29         
 30         try {
 31             input = new BufferedInputStream(new FileInputStream(sourceName));
 32             output = new BufferedOutputStream(new FileOutputStream(targetName));
 33             
 34             int data = -1;
 35             while((data = input.read()) > -1)
 36             {
 37                 output.write(data);
 38             }
 39         } catch (IOException e) {
 40             // TODO Auto-generated catch block
 41             e.printStackTrace();
 42         }finally{
 43             try {
 44                 if(input != null){
 45                     input.close();
 46                 }
 47             } catch (IOException e) {
 48                 // TODO Auto-generated catch block
 49                 e.printStackTrace();
 50             }
 51             try {
 52                 if(output != null){
 53                     output.close();
 54                 }
 55             } catch (IOException e) {
 56                 // TODO Auto-generated catch block
 57                 e.printStackTrace();
 58             }
 59         }
 60         
 61     }
 62     
 63     public static void copyByByteArray(String sourceName,String targetName){
 64         BufferedInputStream input = null;
 65         BufferedOutputStream output = null;
 66         
 67         try {
 68             input = new BufferedInputStream(new FileInputStream(sourceName));
 69             output = new BufferedOutputStream(new FileOutputStream(targetName));
 70             
 71             byte[] data = new byte[1024];
 72             int length = -1;
 73             while((length = input.read(data)) > -1)
 74             {
 75                 output.write(data,0,length);
 76             }
 77         } catch (IOException e) {
 78             // TODO Auto-generated catch block
 79             e.printStackTrace();
 80         }finally{
 81             try {
 82                 if(input != null){
 83                     input.close();
 84                 }
 85             } catch (IOException e) {
 86                 // TODO Auto-generated catch block
 87                 e.printStackTrace();
 88             }
 89             try {
 90                 if(output != null){
 91                     output.close();
 92                 }
 93             } catch (IOException e) {
 94                 // TODO Auto-generated catch block
 95                 e.printStackTrace();
 96             }
 97         }
 98         
 99     }
100 }

 

 

posted @ 2021-02-22 17:58  EvanTheBoy  阅读(127)  评论(0)    收藏  举报