在
CSDN或者ITEYE等网站上,如果直接复制代码,复制的代码中可能有多余的行标。
例如:
class="java">1.public class MyThread implements Runnable {
2. public void run() {
3. for(int i=0; i<10; i++){
4. System.out.println(Thread.currentThread().getName()+":"+i);
5. }
6. }
7.}
8.public class MyThreadClient {
9. public static void main(String[] args){
10. Thread t = new Thread(new MyThread());
11. t.start();
12. for(int i=0; i<10; i++){
13. System.out.println(Thread.currentThread().getName()+":"+i);
14. Thread.yield();
15. }
16. }
17.}
本文就是简单实现一个小程序,目的就是去除这些多余的行标信息。
行标的特点:
空格或者TAB+
数字+
点号
根据这个特点,写一个适合这种情况的
正则表达式==>
^*\\d{1,}+\\.
此正则表达式只适合上面描述的行标,如果复制的代码拥有的行标特点不一样,则需要调整正则表达式。
小程序代码如下:
一个简单的文件过滤器类,主要用于过滤到没有内容的空文件。
package my.tool.file.convertor;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*
* 一个简单的文件过滤器,主要用于过滤到没有内容的空文件。
*
* @author Eric
*
*/
public class EmptyFileFilter implements FileFilter {
public boolean accept(File pathname) {
return !isFileEmpty(pathname);
}
private boolean isFileEmpty(File file) {
FileInputStream fis = null;
boolean flag = true;
try {
fis = new FileInputStream(file);
try {
if (fis.available() != 0) {
flag = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fis) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
fis = null;
}
}
}
return flag;
}
}
一个用于去除行标的类
package my.tool.file.convertor;
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;
public class FileConvertor {
private static final String REGEX_LINE_NUMBER = "^*\\d{1,}+\\.";
public void convert(String srcFolder, String destFolder) {
for (File file : new File(srcFolder).listFiles(new EmptyFileFilter())) {
convertFile(file, destFolder);
System.out.println(file.getName() + " is converted.");
}
}
private void convertFile(File file, String destFolder) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(new File(destFolder, file
.getName())));
String line = null;
while (null != (line = br.readLine())) {
bw.write(removeLineNumber(line));
bw.newLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
br = null;
}
}
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
bw = null;
}
}
}
}
private String removeLineNumber(String value) {
value = value.replaceAll(REGEX_LINE_NUMBER, "");
return value;
}
}
测试代码:
package my.tool.file.convertor;
public class Main {
private static final String DESTINATION_FOLDER = "D:\\DestFolder";
private static final String SOURCE_FOLDER = "D:\\SrcFolder";
public static void main(String[] args) {
System.out.println("Source Folder ==> " + SOURCE_FOLDER);
System.out.println("Destination Folder ==> " + DESTINATION_FOLDER);
System.out.println("Convert Files start.......");
long start = System.currentTimeMillis();
new FileConvertor().convert(SOURCE_FOLDER, DESTINATION_FOLDER);
long end = System.currentTimeMillis();
System.out.println("Convert Files end.......");
System.out.println("Total elapsed time ==> " + (end - start)
+ "ms");
}
}
控制台输出结果如下:
Source Folder ==> D:\SrcFolder
Destination Folder ==> D:\DestFolder
Convert Files start.......
MyTest.java is converted.
MyThread.java is converted.
test.txt is converted.
Convert Files end.......
Total elapsed time ==> 37ms
处理前和处理后的文件内容截图如下:
处理前
处理后
- 大小: 106.8 KB
- 大小: 90.1 KB