java 缓冲区复制文本文件
程序员文章站
2022-03-25 22:45:56
public class CopyTextByBuf { public static void main(String[] args) { BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new Buffere ......
public class copytextbybuf { public static void main(string[] args) { bufferedreader bufr = null; bufferedwriter bufw = null; try { bufr = new bufferedreader(new filereader("demo_src.txt")); bufw = new bufferedwriter(new filewriter("demo_desc.txt")); string line = null; //readline不带行终止符 while ((line = bufr.readline()) != null) { bufw.write(line); bufw.newline(); bufw.flush(); } } catch (ioexception e) { throw new runtimeexception("读写失败!"); } finally { try { if (bufr != null) bufr.close(); } catch (ioexception e) { throw new runtimeexception("读取关闭失败!"); } try { if (bufw != null) bufw.close(); } catch (ioexception e) { throw new runtimeexception("写入关闭失败!"); } } } }
newline()
无论读一行还是获取多个字符,其实最终都是在硬盘上一个一个读取。所以最终使用的还是read()一次读一个的方法。
上一篇: day003-python初识
下一篇: STL--标准模板库--简要概述