问题遇到的现象和发生背景
用缓冲字节流实现文件复制的功能
创建a.txt文件,然后把a.txt文件复制成acopy.txt
a.txt文件中的内容未能成功复制到acopy.txt中,acopy.txt中为空白用代码块功能插入代码,请勿粘贴截图
@Test
public void test5() {BufferedReader br = null; BufferedWriter bw = null; BufferedWriter bw1 = null; try { bw1 = new BufferedWriter(new FileWriter("d:\\c.txt")); bw1.write("dasdsadadadsllll"); br = new BufferedReader(new FileReader("d:\\c.txt")); bw = new BufferedWriter(new FileWriter("d:\\copy4.txt")); int len; char[] cbuf=new char[5]; while ((len=br.read(cbuf))!=-1){ bw.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if(bw!=null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(bw1!=null){ try { bw1.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
运行结果及报错内容
无法成功复制内容