将此代码拷入程序里,调用该方法即可
srcPath:源文件路径
destPath:目标文件路径
public void copyFileWithBuffered(String srcPath, String destPath) {
BufferedInputStream bfis = null;
BufferedOutputStream bfos = null;
try {
//1 原文件对象
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2 流对象
//3节点流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
//4 缓冲流
bfis = new BufferedInputStream(fis);
bfos = new BufferedOutputStream(fos);
//5 读写操作
byte[] buffer = new byte[1024];
int len;
while ((len = bfis.read(buffer)) != -1) {
bfos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//6 关闭外层流,内层节点流会自动关闭
if (bfis != null) {
try {
bfis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bfos != null) {
try {
bfos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}