[b][size=medium][color=indigo]今天在一个讨论群中有人问了一个问题,要去掉文件中的汉字等特殊字符,只保留数字,写了个小程序实现这个功能[/color][/size][/b]
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
/**
* 读取一个txt文件,去掉其中的汉字,剩下每个数字串存储成一行,存到新文件中
* @author huangqun
*/
public class KeepNumber {
public static void main(String[] args) {
String regExp = "[^0-9]*";//正则表达式 非数字
List<String> cacheList = new ArrayList<String>(); //缓存list 用于存放处理后的字符串 每行是一个字符串放到list中
try {
//1.读取文件
String readStr = null;
File file = new File("E:/1.txt");//要处理的文件路径 FIXME
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while(null != (readStr = br.readLine())) {
if("".equals(readStr.trim())) continue;
readStr = readStr.replaceAll(regExp, "");//替换操作
cacheList.add(readStr);
}
br.close();isr.close();is.close();
//2.把cacheList写到新文件中
String outPath = file.getAbsolutePath();
/**
* 组装新生成的备份文件路径
*/
outPath = outPath.substring(0, outPath.indexOf(".")) + "bak." + outPath.substring(outPath.indexOf(".")+1, outPath.length());
File outFile = new File(outPath);
if (!outFile.exists()) {
outFile.createNewFile();
}
OutputStream os = new FileOutputStream(outFile);
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
for (String str : cacheList) {
System.out.print(str + "\r\n");
bw.write(str + "\r\n");
}
bw.flush();osw.close();os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}