JAVA(IO流)文件复制

时间:2021-04-11 20:59:04
 1 package com.am;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 public class TuPian {
 8 
 9     public static void main(String[] args) {
10         FileInputStream f1 = null;
11         FileOutputStream f2 = null;
12         try {
13             f1 = new FileInputStream("E:\\5s.JPG");
14             f2 = new FileOutputStream("E:\\6s.jpg");
15             byte[] bytes = new byte[1024];
16         int len = 0;
17         while ((len=f1.read(bytes))!=-1) {
18             f2.write(bytes,0,len);
19         }
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23         if (f1!=null) {
24             try {
25                 f1.close();
26             } catch (IOException e) {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }
30         }
31         if (f2!=null) {
32             try {
33                 f2.close();
34             } catch (IOException e) {
35                 // TODO Auto-generated catch block
36                 e.printStackTrace();
37             }
38         }
39     }
40 }