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()一次讀一個的方法。