使用文件输入输出流完成文件的复制

这里用前面学到的东西完成文件的复制(从一个地方复制到另一个地方)

以下是按照字节来读取:

 1 package com.hw.file0205;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class StreamTest {
 9     public static void main(String[] args) {
10         
11         copyByByte("F://骚操作//demo02_test.txt","F://骚操作//demo03_test.txt");  //复制文本文件
12         copyByByte("E://JavaScript//JS//img//testImg.jpg", "F://骚操作//overImg.jpg");  
13         //复制图片文件
14     }
15     
16     //使用下面这个静态方法完成文件的复制。
17     public static void copyByByte(String sourceName,String targetName){
18         FileInputStream input = null;
19         FileOutputStream output = null;
20         try {
21             input = new FileInputStream(sourceName);
22             output = new FileOutputStream(targetName);
23             
24             int data = -1;  //按照字节来读取,这里data就是读取到的字节
25             while((data = input.read()) > -1)  //大于-1说明未到文件末尾,可以继续读取
26             {
27                 output.write(data);  //这样就是一边读一边写
28             }
29             
30         } catch (IOException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }finally{
34             //input和output的关闭必须放在两个try-catch语句中
35             try {
36                 if(input != null){
37                     input.close();
38                 }
39             } catch (IOException e) {
40                 // TODO Auto-generated catch block
41                 e.printStackTrace();
42             }
43             try {
44                 if(output != null){
45                     output.close();
46                 }
47             } catch (IOException e) {
48                 // TODO Auto-generated catch block
49                 e.printStackTrace();
50             }
51         }
52     }
53 }

 

 第二种方法:按照字节数组来读取:

 1 package com.hw.file0205;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class StreamTest {
 9     public static void main(String[] args) {
10         
11         copyByByteArray("F://骚操作//demo02_test.txt","F://骚操作//demo03_test.txt");  //复制文本文件
12         copyByByteArray("E://JavaScript//JS//img//testImg.jpg", "F://骚操作//overImg.jpg");  
13         //复制图片文件
14     }
15     
16     //使用下面这个静态方法完成文件的复制。
17     public static void copyByByteArray(String sourceName,String targetName){
18         FileInputStream input = null;
19         FileOutputStream output = null;
20         try {
21             input = new FileInputStream(sourceName);
22             output = new FileOutputStream(targetName);
23             
24             byte[] data = new byte[1024];
25             int length = -1;  //获取长度
26             while((length = input.read(data)) > -1)  
27             {
28                 output.write(data,0,length);  //指定写入的范围
29             }
30             
31         } catch (IOException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }finally{
35             //input和output的关闭必须放在两个try-catch语句中
36             try {
37                 if(input != null){
38                     input.close();
39                 }
40             } catch (IOException e) {
41                 // TODO Auto-generated catch block
42                 e.printStackTrace();
43             }
44             try {
45                 if(output != null){
46                     output.close();
47                 }
48             } catch (IOException e) {
49                 // TODO Auto-generated catch block
50                 e.printStackTrace();
51             }
52         }
53     }
54 }

 这两种方法虽然都可以完成对文件的复制,但差别其实主要体现在其运行效率上,我们可以通过System.currentTimeMillis()来获取当前毫秒数,来看看两种方法的运行效率的差别:

  1 package com.hw.file0205;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 
  8 public class StreamTest {
  9     public static void main(String[] args) {
 10         long start1,start2,end1,end2;
 11         
 12         start1 = System.currentTimeMillis();
 13         copyByByteArray("E://JavaScript//JS//img//testImg.jpg","F://骚操作//over.jpg");  
 14         end1 = System.currentTimeMillis();
 15         System.out.println("字节数组方式需要的毫秒数:"+(end1-start1));
 16         
 17         start2 = System.currentTimeMillis();
 18         copyByByte("E://JavaScript//JS//img//testImg.jpg","F://骚操作//overImg.jpg");  
 19         end2 = System.currentTimeMillis();
 20         System.out.println("字节方式需要的毫秒数:"+(end2-start2));
 21     }
 22     
 23     //利用字节数组
 24     public static void copyByByteArray(String sourceName,String targetName){
 25         FileInputStream input = null;
 26         FileOutputStream output = null;
 27         try {
 28             input = new FileInputStream(sourceName);
 29             output = new FileOutputStream(targetName);
 30             
 31             byte[] data = new byte[1024];
 32             int length = -1;  //获取长度
 33             while((length = input.read(data)) > -1)  
 34             {
 35                 output.write(data,0,length);  //指定写入的范围
 36             }
 37             
 38         } catch (IOException e) {
 39             // TODO Auto-generated catch block
 40             e.printStackTrace();
 41         }finally{
 42             //input和output的关闭必须放在两个try-catch语句中
 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 copyByByte(String sourceName,String targetName){
 64         FileInputStream input = null;
 65         FileOutputStream output = null;
 66         try {
 67             input = new FileInputStream(sourceName);
 68             output = new FileOutputStream(targetName);
 69             
 70             
 71             int data = -1;  
 72             while((data = input.read()) > -1)  
 73             {
 74                 output.write(data);  
 75             }
 76             
 77         } catch (IOException e) {
 78             // TODO Auto-generated catch block
 79             e.printStackTrace();
 80         }finally{
 81             //input和output的关闭必须放在两个try-catch语句中
 82             try {
 83                 if(input != null){
 84                     input.close();
 85                 }
 86             } catch (IOException e) {
 87                 // TODO Auto-generated catch block
 88                 e.printStackTrace();
 89             }
 90             try {
 91                 if(output != null){
 92                     output.close();
 93                 }
 94             } catch (IOException e) {
 95                 // TODO Auto-generated catch block
 96                 e.printStackTrace();
 97             }
 98         }
 99     }
100 }

 

posted @ 2021-02-21 15:50  EvanTheBoy  阅读(374)  评论(0)    收藏  举报