文件拷贝
应用实例:编程完成图片/音乐的拷贝
public class FileCopy {
public static void main(String[] args) {
String srcFilePath = "D:\\Jay.jpg";
String destFilePath = "D:\\Jay2.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] buf = new byte[1024];
int readLen = 0;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
while ((readLen = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝ok");
} catch (IOException e) {
e.printStackTrace();
} finally {
//流不等于空才需要关闭,有流才需要关闭
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}