---
title: "用文件类实现复制功能"
author: "suyixiang"
date: "2012-12-21T10:52:55Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-12-21T10:52:55Z"
id: 1751572
url: "https://suyixiang.iteye.com/blog/1751572"
---

01. /*
02. * couter:苏奕祥
03. * 实现两个文件的复制功能并且用到正则表达式替换复制完后的值
04. \*
05. \*/
06. import  java.io.\*;
07. import  java.util.regex.Matcher;
08. import  java.util.regex.Pattern;
09. 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. }