网页复制代码去行标

在CSDN或者ITEYE等网站上,如果直接复制代码,复制的代码中可能有多余的行标。

例如:
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.}


本文就是简单实现一个小程序,目的就是去除这些多余的行标信息。

[b]行标的特点[/b]:[color=blue][b]空格或者TAB[/b][/color]+[color=red][b]数字[/b][/color]+[color=red][b]点号[/b][/color]

根据这个特点,写一个适合这种情况的正则表达式==> [color=blue][b]^*\\d{1,}+\\.[/b][/color]

[b]此正则表达式只适合上面描述的行标,如果复制的代码拥有的行标特点不一样,则需要调整正则表达式。[/b]

小程序代码如下:

一个简单的文件过滤器类,主要用于过滤到没有内容的空文件。

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

处理前和处理后的文件内容截图如下:
处理前

[img]http://dl2.iteye.com/upload/attachment/0087/3040/94e28c37-82bd-3c11-a382-d21a13d314b9.jpg[/img]

处理后

[img]http://dl2.iteye.com/upload/attachment/0087/3042/108ef51a-658f-31f1-9b9b-ddc39b45cda8.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值