使用字节流复制非文本文件(对图片的复制)
/**
* 使用字节流复制非文本文件
* 对图片的复制
*/
@Test
public void test1() {
//输入字节流对象
FileInputStream fileInputStream = null;
//输出字节流对象
FileOutputStream fileOutputStream = null;
try {
//源文件
File srcFile = new File("图片1.png");
//目标文件
File destFile = new File("图片2.png");
fileInputStream = new FileInputStream(srcFile);
fileOutputStream = new FileOutputStream(destFile);
//读取源文件的字节数组
byte[] bytes = new byte[100];
int len;
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭输出字节流
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
//关闭输入字节流
try {
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}