用资料类实现复制功能
用文件类实现复制功能
- /*
- * couter:苏奕祥
- * 实现两个文件的复制功能并且用到正则表达式替换复制完后的值
- *
- */
- import java.io.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class CopyFile {
- public static void main(String[] args) throws IOException{
- File f=new File( "E:\\sdx.txt" );
- File f2=new File( "E:\\sdxcopy.txt" );
- Writer copy=new FileWriter(f2);
- Writer aaa=null ;
- aaa=new FileWriter(f);
- 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" );
- aaa.close();
- Reader read=null ;
- read=new FileReader(f);
- char c[]= new char [ 1024 ];
- int len=read.read(c);
- System.out.println(len);
- [color=blue][/color] read.close();
- String str=new String(c, 0 ,len);
- System.out.println("内容为:" +str);
- Pattern p=Pattern.compile("LOVE" );
- Matcher m=p.matcher(str);
- String str2=m.replaceAll("HATE" );
- System.out.println(str2);
- copy.write(str2);
- copy.close();
- }
- }