完成文件拷贝,将 e:\Koala.jpg 拷贝到d:\
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/576b7d86b0744212c025c54f2c347fe4.png)
package com.cyr.IOStream.outputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
String srcFilePath = "e:\\Koala.jpg";
String destFilePath = "d:\\Koala.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝成功~");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null){
fileInputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}