|
去掉代码文件中每行开头的行号(带空格),像四个数字接一个冒号的.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
Pattern pattern = Pattern.compile("(?m)^[ \t]*\\d{4}:[ \t]*");//多行匹配模式
String line = System.getProperty("line.separator");
br = new BufferedReader(new FileReader("1.txt"));
bw = new BufferedWriter(new FileWriter("1_.txt"));
String str = null;
while((str = br.readLine())!=null) {
str = replace(pattern, str);
bw.write(str+line);
}
}catch(IOException e) {
e.printStackTrace();
}finally{
close(bw);
close(br);
}
System.out.println("finished.");
}
private static String replace(Pattern pattern, String str) {
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll("");
}
private static void close(Closeable closeable) {
if(closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.txt文件内容
0001: Line 1, 0001: Line 1
0002: Line 2, 0002: Line 2
0003: Line 3, 0003: Line 3
0004: Line 4, 0004: Line 4 |
|