黑马程序员-----字符流copy文本文档(黑马视频)

来源:互联网 发布:优麒麟和windows 编辑:程序博客网 时间:2024/06/08 15:23

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

package src.com.itheima.ioDemo;

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

public class CopyFileDome {
 public static void main(String[] args) {
  copyName("K:\\demo.txt");
 }
 public static void copyName(String str){
  FileReader fr=null;
  FileWriter fw=null;
  try {
   fr=new FileReader(str);
   fw=new FileWriter("K:\\java学习笔记\\demo.txt",true);
   int fr_ch=0;
   char [] c=new char[1024];
   while((fr_ch=fr.read(c))!=-1){
    fw.write(c,0,fr_ch);
   }
   
  } catch (Exception e) {
   throw new RuntimeException("读写失败");
  }finally{
   try {
    if(fr!=null)
    fr.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   try {
    if(fw!=null)
    fw.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
 }
}

0 0
原创粉丝点击