ITeye博客

用文件类实现复制功能

suyixiang · 2012-12-21 10:52 · JAVA基础 · 编程语言 · Markdown

  1. /*
  2. * couter:苏奕祥
  3. * 实现两个文件的复制功能并且用到正则表达式替换复制完后的值
  4. *
  5. */
  6. import  java.io.*;
  7. import  java.util.regex.Matcher;
  8. import  java.util.regex.Pattern;
  9. public   class  CopyFile {
  10. public   static   void  main(String[] args) throws  IOException{
  11. File f=new  File( "E:\\sdx.txt" );
  12. File f2=new  File( "E:\\sdxcopy.txt" );
  13. Writer copy=new  FileWriter(f2);
  14. Writer aaa=null ;
  15. aaa=new  FileWriter(f);
  16. aaa.write("I LOVE U I LOVE U I LOVE U\r\nI LOVE U I LOVE U I LOVE U\r\nI LOVE U I LOVE U I LOVE U" );
  17. aaa.close();
  18. Reader read=null ;
  19. read=new  FileReader(f);
  20. char  c[]= new   char [ 1024 ];
  21. int  len=read.read(c);
  22. System.out.println(len);
  23. [color=blue][/color]        read.close();
  24. String str=new  String(c, 0 ,len);
  25. System.out.println("内容为:" +str);
  26. Pattern p=Pattern.compile("LOVE" );
  27. Matcher m=p.matcher(str);
  28. String str2=m.replaceAll("HATE" );
  29. System.out.println(str2);
  30. copy.write(str2);
  31. copy.close();
  32. }
  33. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。