支持版本
JDK1.7及以上
好处
可以自己帮你关闭IO流,不用手动写,不用try catch 套try catch的写,搞得很难看;
代码如下
这里做演示和记录,printStackTrace就不做处理了
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\YvYv\\Desktop\\13.png")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\YvYv\\Desktop\\66.png")));
) {
byte[] bytes = new byte[bis.available()];
int len;
while ((len = bis.read(bytes))!=-1) {
bos.write(bytes,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}