JavaSE-18.2.6【案例-字符缓冲流复制Java文件(基础版实现、基于字符缓冲流特有功能的改进版实现)】
1 package day9.lesson2; 2 3 import java.io.*; 4 5 /* 6 2.10 案例-字符缓冲流复制Java文件 7 */ 8 public class CopyJavaDemo03 { 9 public static void main(String[] args) throws IOException { 10 BufferedReader br = new BufferedReader(new FileReader("stage2\\src\\day9\\lesson2\\StringDemo.java")); 11 BufferedWriter bw = new BufferedWriter(new FileWriter("stage2\\day9_lesson2_copy\\CopyStringDemo03.java")); 12 13 //方式1 14 /*int ch; 15 while ((ch=br.read()) != -1){ 16 bw.write(ch); 17 }*/ 18 19 //方式2 20 char[] chars = new char[1024]; 21 int len; 22 while ((len=br.read(chars)) != -1){ 23 // bw.write(chars); 24 bw.write(chars, 0, len); 25 } 26 27 bw.close(); 28 br.close(); 29 } 30 }
1 package day9.lesson2; 2 3 import com.sun.xml.internal.ws.api.ha.StickyFeature; 4 5 import java.io.*; 6 7 /* 8 2.12 案例-使用字符缓冲流特有功能复制Java文件 9 */ 10 public class CopyJavaDemo04 { 11 public static void main(String[] args) throws IOException { 12 BufferedReader br = new BufferedReader(new FileReader("stage2\\src\\day9\\lesson2\\StringDemo.java")); 13 BufferedWriter bw = new BufferedWriter(new FileWriter("stage2\\day9_lesson2_copy\\CopyStringDemo04.java")); 14 15 String line; 16 while ((line=br.readLine()) != null){ 17 bw.write(line); 18 bw.newLine(); //手动换行,因为.readLine()不读取原文件的换行符等,只读取每行的内容 19 bw.flush(); 20 } 21 22 bw.close(); 23 br.close(); 24 } 25 }