JAVA 文件拷贝(基本字节流)--java(六)

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 /**
 8  *     文件拷贝(基本字节流)
 9  *
10  */
11 public class IOtest03 {
12     public static void main(String[] args) {
13         copyFile("D:\\JAVA重拾\\test1\\test01\\headImg.jpg", "D:\\JAVA重拾\\test1\\test01\\cc.jpg");
14     }
15     
16     public static void copyFile(String yuan,String fu) {
17         File file = new File(yuan);
18         File files = new File(fu);
19         FileInputStream fis = null;
20         FileOutputStream fos = null;
21         try {
22             fis = new FileInputStream(file);
23             fos = new FileOutputStream(files);
24             int a ;
25             byte[] flush = new byte[1024];//缓冲容器
26             while((a = fis.read(flush)) != -1) {
27                 String str = new String(flush,0,a);    //批量解码
28                 fos.write(flush);
29                 fos.flush();
30             }
31         } catch (FileNotFoundException e) {
32             e.printStackTrace();
33         } catch (IOException e) {
34             e.printStackTrace();
35         }finally {
36             try {
37                 if(fos!=null) {
38                     fos.close();
39                 }
40                 if(fis!=null) {
41                     fis.close();
42                 }
43             } catch (IOException e) {
44                 e.printStackTrace();
45             }
46         }
47     }
48 }

 

posted @ 2020-06-12 16:58  梅竹疯狂打豆豆  阅读(200)  评论(0)    收藏  举报