package com.huang;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RExpre {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String line; //用来保存读取的内容
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream("E:\\development\\MyEclipse 10\\Exam\\RExpress.txt")));
//读文本文件
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\development\\MyEclipse 10\\Exam\\B.txt")));
//写文本文件
line=reader.readLine(); //读取第一行
StringBuffer sb=new StringBuffer();
while(line!=null)
{
sb.append(line);
String str="\\d{2}/+\\d{2}/+\\d{4}" ;
Pattern p=Pattern.compile(str);
Matcher m=p.matcher(sb);
while(m.find())
{
String s1=sb.substring(m.start(), m.end());
String[] p2=s1.split("/"); //将一个字符串分割为子字符串,然后将结果作为字符串数组返回
sb.replace(m.start(), m.end(), p2[2]+"年"+p2[0]+"月"+p2[1]+"日"); //把找到的sb字符串中的字符串替换为xxxx年xx月xx日的形式
}
System.out.println(sb);
writer.write(sb.toString());
sb.delete(0, sb.length()); //把sb清空
line=reader.readLine(); //读取下一行
}
reader.close(); //关掉,否则会发生资源泄露
writer.close();
}
}
原始文本内容:
ndfsnnn02/24/2012nihaohaoaoaoadfsnnn02/21/2011nihaohaoa
nnndsdn02/24/2012fsdafsafdfsnnn02/24/2012nihaohaoa
nnewqenn02/24/2012hgfh
nn234nn02/24/2012hfgfhfhfg
ndfsnnn02/24/2012nihaohaoaoaoa
nnndsdn2012年2月24日fsdafsaf
nnewqenn2012年2月24日hgfh
nn234nn2012年2月24日hfgfhfhfg
处理后文本的内容:
ndfsnnn2012年02月24日nihaohaoaoaoadfsnnn2011年02月21日nihaohaoa
nnndsdn2012年02月24日fsdafsafdfsnnn2012年02月24日nihaohaoa
nnewqenn2012年02月24日hgfh
nn234nn2012年02月24日hfgfhfhfg
ndfsnnn2012年02月24日nihaohaoaoaoa
nnndsdn2012年2月24日fsdafsaf
nnewqenn2012年2月24日hgfh
nn234nn2012年2月24日hfgfhfhfg