步骤:
- 创建输入流
- 创建输出流
- 创建缓冲数组
- 文件复制
- 关闭输入流
- 关闭输出流
@Test
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream("E:\\download\\E-R图.png");
FileOutputStream fos = new FileOutputStream("E:\\E-R图.png");
byte[] buf = new byte[1024 * 1024];
int bytesRead;
while ((bytesRead = fis.read(buf)) > 0) {
fos.write(buf, 0, bytesRead);
}
fis.close();
fos.close();
}
@Test
public void copyFile2() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("E:\\download\\E-R图.png");
fos = new FileOutputStream("E:\\E-R图.png");
byte[] buf = new byte[1024 * 1024];
int bytesRead;
if ((bytesRead=fis.read(buf))>0){
fos.write(buf,0,bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fis.close();
fos.close();
}
}