考试第四题
package test04; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class Demo { //在C盘下创建一个文件,名称为javabigdata,在此文件夹下创建一个a.txt的文件 //,并写入如下内容:I love china,I love beiijng,the Beijing //is the capatial of China., //然后将a.txt复制到此文件夹下为b.txt //1、地址 2、读取 Read FileRead BUfferRead 输出 Write FileOut BufferWriter public static void main(String[] args) { Reader re = null; BufferedReader br = null; Writer wi = null; BufferedWriter bw = null; try { re = new FileReader("C:/javabigdata/a.txt"); br = new BufferedReader(re); try { wi = new FileWriter("C:/javabigdata/b.txt"); bw = new BufferedWriter(wi); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String line = ""; try { while((line=br.readLine())!=null) { bw.write(line); System.out.println("复制成功"); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bw.close(); wi.close(); br.close(); re.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }