日期:2014-05-19 浏览次数:20836 次
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.lang.Exception; import java.util.regex.*; public class pattern { public static void main(String[] args) { File f111 = new File("F:/JAVA代码存储/文件提取汉字/111.txt"); File f222 = new File("F:/JAVA代码存储/文件提取汉字/222.txt"); try{ BufferedReader reader=new BufferedReader(new FileReader(f111)); //缓冲流 StringBuilder sb=new StringBuilder(); //将所有源码按格式读入sb中 StringBuilder sb2=new StringBuilder(); //将所有匹配出的源码按格式写入sb2中 BufferedWriter bw=new BufferedWriter(new FileWriter(f222)); //缓冲流 String str,str2; //负责读每一行源码 while((str=reader.readLine())!=null) { str=str+"\n"; sb.append(str); } String reg = "[\u4e00-\u9fa5](.|[\u4e00-\u9fa5])*。"; Pattern pat = Pattern.compile(reg); System.out.println(0); Matcher mat = pat.matcher(sb.toString()); System.out.println(mat.find()); if(mat.find()==false) {System.out.println("没有匹配到");} if(mat.find()==true) {System.out.println("匹配到");} System.out.println(1); while(mat.find()) { str2 = mat.group(); if(str2.length()<1) continue; System.out.println(str2); str2 = str2+"\n"; sb2.append(str2); } System.out.println(2); String sbb; sbb = sb2.toString(); bw.write(sbb); reader.close(); bw.close(); } catch(FileNotFoundException e1){}catch(IOException e2){} catch(PatternSyntaxException e3){System.out.println("语法错误");} System.out.println("完成"); } }