我是靠谱客的博主 执着小白菜,这篇文章主要介绍IO流--输入字节流(FileInputStream)和输出字节流(FileOutputStream),现在分享给大家,希望可以做个参考。

使用字节流复制非文本文件(对图片的复制)


/**
* 使用字节流复制非文本文件
* 对图片的复制
*/
@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();
}
}
}

最后

以上就是执着小白菜最近收集整理的关于IO流--输入字节流(FileInputStream)和输出字节流(FileOutputStream)的全部内容,更多相关IO流--输入字节流(FileInputStream)和输出字节流(FileOutputStream)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部