码迷,mamicode.com
首页 > 编程语言 > 详细

Java复制文件

时间:2018-04-12 13:35:24      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:int   throw   存在   sts   put   new   except   read   MF   

 1  public void copyFile(File fromFile,File toFile) throws IOException{
 2         //如果目标文件不存在,则创建
 3         File parentFile = toFile.getParentFile();
 4         if(!parentFile.exists()){
 5             parentFile.mkdirs();
 6         }
 7         if(!toFile.exists()){
 8             toFile.createNewFile();
 9         }
10         //复制文件
11         FileInputStream ins = new FileInputStream(fromFile);
12         FileOutputStream out = new FileOutputStream(toFile);
13         byte[] b = new byte[1024];
14         int n=0;
15         while((n=ins.read(b))!=-1){
16             out.write(b, 0, n);
17         }
18         
19         ins.close();
20         out.close();
21     }

 

Java复制文件

标签:int   throw   存在   sts   put   new   except   read   MF   

原文地址:https://www.cnblogs.com/tashaxing/p/8806582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!