Document

文件拷贝的工具类实现

 1 import java.io.BufferedInputStream;
 2 import java.io.BufferedOutputStream;
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 
 6 public class TestDemo {
 7 
 8     public  void copyFile(String str,String ssr){
 9         FileInputStream fis = null;
10         FileOutputStream fos = null;
11         BufferedInputStream bis = null;
12         BufferedOutputStream bos = null;
13         try {
14                 bis = new BufferedInputStream(new FileInputStream(str));
15                 bos = new BufferedOutputStream(new FileOutputStream(ssr));
16                 int temp = 0;
17                 while((temp = bis.read())!=-1){
18                     bos.write(temp);
19                 }
20                 bos.flush();
21         }catch (Exception e){
22             e.fillInStackTrace();
23         }finally {
24             try {
25                 //注意关闭顺序,后进先关
26                 if (bis != null){
27                     bis.close();
28                 }
29                 if (fis != null){
30                     fis.close();
31                 }
32                 if (bos != null){
33                     bos.close();
34                 }
35                 if (fos != null){
36                     fos.close();
37                 }
38             }catch (Exception e){
39                 e.fillInStackTrace();
40             }
41         }
42     }
43 
44 
45 
46 }

 

直接调用即可

posted @ 2022-08-23 20:06  一蓑烟雨任平生。。  阅读(24)  评论(0)    收藏  举报
Document