IO流之封装文件复制
实现两种功能的封装
1、封装拷贝(基于IO流的InputStream
和OutputStream
)
2、封装释放资源
封装拷贝
可以从文件到文件,也可以从文件到字节数组,或从字节数组到文件。
示例代码如下:
/**
* 拷贝
* 对接输入,输出流
* @param is
* @param os
*/
public static void copy(InputStream is, OutputStream os) {
try {
byte[] flush = new byte[3];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(is, os);
}
}
封装释放资源
使用IO流中共同实现的Closeable对所有的流进行关闭
/**
* 释放资源
*
* @param ios
*/
public static void close(Closeable... ios) {
for (Closeabl