`

用文件类实现复制功能

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

相关推荐

Global site tag (gtag.js) - Google Analytics