java复制文件

 1 /**
 2      * @param sourceFile
 3      * @param targetFile
 4      * @throws IOException
 5      */
 6     public static void copyFile(File sourceFile, File targetFile)
 7             throws IOException {
 8         BufferedInputStream inBuff = null;
 9         BufferedOutputStream outBuff = null;
10         try {
11             inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
12             outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
13 
14             byte[] b = new byte[1024 * 1024];
15             int len;
16             while ((len = inBuff.read(b)) != -1) {
17                 outBuff.write(b, 0, len);
18             }
19             outBuff.flush();
20         } finally {
21             if (inBuff != null)
22                 inBuff.close();
23             if (outBuff != null)
24                 outBuff.close();
25         }
26     }

 

posted on 2014-10-23 15:52  hellofking  阅读(146)  评论(0编辑  收藏  举报

导航