Skip to content

Instantly share code, notes, and snippets.

@inmyfree
Last active December 14, 2015 09:09
Show Gist options
  • Save inmyfree/5063225 to your computer and use it in GitHub Desktop.
Save inmyfree/5063225 to your computer and use it in GitHub Desktop.
public static void copyFile(String f1, String f2) throws IOException {
File sourceFile = new File(f1);
File targetFile = new File(f2);
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment