- 方法一:读到指定字符数组
@Test public void test7(){ BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader("射雕英雄传.txt")); bw = new BufferedWriter(new FileWriter("射雕英雄传1.txt")); char[] cbuf = new char[1024]; int len; 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(); } } } }
- 方法2:一次读一行
@Test public void test8(){ BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader("射雕英雄传.txt")); bw = new BufferedWriter(new FileWriter("射雕英雄传1.txt")); String data; while ((data = br.readLine()) != null){ bw.write(data);//不包括换行 bw.newLine();//换行 } } 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(); } } } }