FileOutputStream out = null; OutputStreamWriter writer = null; try { out = new FileOutputStream("OutputStream.txt"); writer = new OutputStreamWriter(out);//字节流转换为字符流 String s = "hello world 字节流";//字符 writer.write(s); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(writer!=null)writer.close(); if(out!=null)out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //字节流文件续写 try { out = new FileOutputStream("OutputStream.txt",true); writer = new OutputStreamWriter(out);//字节流转换为字符流 String s2 = " 续写文件";//字符 writer.write(s2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(writer!=null)writer.close(); if(out!=null)out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }在这个程序的最后我分别作了字符流和字节流的关闭操作,分别是writer.close();out.close();但看完缓冲区的视频后,老师指明 BufferWriter.close()就已经关闭了字符流,所以不需要再惊醒Writer.close()所以不禁想起了上面那个问题是否只关闭字符流就可以了?坐等答案哦,欢迎在线讨论
|
|