2021-09-25 2025-10-22 Java 完成⼀个⽂件的拷⻉ 写法⼀(以前内容) 123456789101112131415161718try { FileInputStream fis = new FileInputStream("xdclass.txt"); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream("copy.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); int size; byte[] buf = new byte[1024]; while ((size = bis.read(buf)) != -1) { bos.write(buf, 0, size); } //刷新此缓冲的输出流,才可以保证数据全部输出完成,close会⾃动关闭 //bos.flush(); //关闭的时候只需要关闭最外层的流就⾏了,源码⾥⾯会⾃动关闭inputstream对象的 bis.close(); bos.close(); } catch (Exception e) { e.printStackTrace(); } 写法⼆(JDK6之前,⼤部分⼈还停留这个写法) 12345678910111213141516171819202122232425262728293031BufferedInputStream bis = null;BufferedOutputStream bos = null;try {FileInputStream fis = new FileInputStream("xdclass.txt");bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream("copy.txt");bos = new BufferedOutputStream(fos);int size;byte[] buf = new byte[1024];while ((size = bis.read(buf)) != -1) {bos.write(buf, 0, size);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {e.printStackTrace();} finally {if (bos != null) {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}}} 前一篇 新版JDK try-with-resource处理IO异常 后一篇 字符流和字节流的桥梁OutputStreamWriter
说些什么吧!