ReadAndWriteFile

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

/**
* 把test1.txt中的内容控制台输出,并把其内容写入test2.txt
* @author xxx
*/

public class ReadAndWriteFile {

 public static void main(String[] args) {
  BufferedReader bf = null;
  BufferedWriter bw = null;
  String line = null;

  try {
   bf = new BufferedReader(new FileReader("E:\\test1.txt"));
   bw = new BufferedWriter(new FileWriter("E:\\test2.txt"));
   while ((line = bf.readLine()) != null) {
    System.out.println(line);
    bw.write(line);
    bw.newLine();
    bw.flush();
   }
  } catch (FileNotFoundException e) {
   System.out.println("找不到指定文件!!");
  } catch (IOException e) {
   System.out.println("文件读取有误!");
  } finally {
   try {
    bf.close();
    bw.close();
   } catch (IOException e) {
    System.out.println(e.getMessage());
   }
  }
 }
}

阅读更多
文章标签: import string class null
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭