java 正则表达式进行剔除字符
package com.regular;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class testRegular {
private static Pattern pattern = Pattern.compile("^\\d{1,3}\\.");
private static Matcher matcher = null;
public static StringBuffer removeOtherCharacter(File file){
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String line = new String();
line = reader.readLine();
while(line != null){
buffer.append(remove(line));
buffer.append("\n\r");
line = reader.readLine();
}
return buffer;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static String remove(String source){
matcher = pattern.matcher(source);
return matcher.replaceAll("");
}
public static void writeSourceFile(StringBuffer buffer){
File file = new File("src/com/regular/file_new.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(buffer.toString());
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("src/com/regular/file.txt");
StringBuffer buffer = removeOtherCharacter(file);
writeSourceFile(buffer);
}
}