信息提取小程序
作者:vashon
时间:2017-01-12
需求:
批量统计整个工程代码中满足条件的文件且文件内容需要翻译的中文信息的提取,日志输出文件需要翻译的信息位置及去除重复需要翻译的信息。
代码片段:
package com.search;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 指定某个工程下,指定文件中满足一定条件的中文信息.
*
*/
public class QueryZh {
/** 换行. **/
private final static String NEW_LINE = "\r\n";
/** 保存不重复的需要翻译的信息. **/
private static Set<String> allStrings = new HashSet<String>();
/**
* 合并重复需要翻译的内容.
*
* @param path
* 源文件路径
*/
public static void mergeFile(String path) {
// 修改文件名称
File targetFile = new File(path.substring(0, path.lastIndexOf(".")) + "_combine.txt");
BufferedWriter bw = null;
if (!targetFile.exists()) {
try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bw = new BufferedWriter(new FileWriter(targetFile, true));
for (String str : allStrings) {
String content = "需要翻译的信息:[" + str + "]";
System.out.println(content);
bw.write(content);
bw.write(NEW_LINE);
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
/**
* 查找指定文件的中文字符串. <br>
* 满足一定条件的需要转码的中文字符串.
*
* @param srcPath
* 源路径
* @param targetPath
* 目标存储路径
*/
public static void search(String srcPath, String targetPath) {
File srcFile = new File(srcPath);
File targetFile = new File(targetPath);
if (!targetFile.exists()) {
try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 查找匹配中文的正则
Pattern p = Pattern.compile("([\u4e00-\u9fa5]+)");
if (!srcFile.exists()) {
return;
} else {
BufferedReader br = null;
BufferedWriter bw = null;
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(srcFile));
br = new BufferedReader(reader);
String line = "";
String mv = null;
int lineSize = 0;
while ((line = br.readLine()) != null) {
String content = line;
Matcher m = p.matcher(content);
lineSize++;
while (m.find()) {
mv = m.group(0);
int index = content.indexOf(mv);
// 中文字符的开始
if (content.substring(index - 1).startsWith("\"")) {
String mvContent = "文件位置:" + srcPath + ";第" + lineSize + "行;需要翻译的中文:-->[" + mv + "]";
System.out.println(mvContent);
bw = new BufferedWriter(new FileWriter(targetFile, true));
bw.write(mvContent);
allStrings.add(mv);
bw.write(NEW_LINE);
bw.flush();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
/**
* 获取指定路径下的所有符合条件的路径.
*
* @param path
* 路径
* @param srcSuffix
* 后缀名
* @return 文件路径集合
*/
public static List<String> listPath(File path, String srcSuffix) {
List<String> list = new ArrayList<String>();
File[] files = path.listFiles();
Arrays.sort(files);
for (File file : files) {
if (file.isDirectory()) {// 如果是目录
List<String> _list = listPath(file, srcSuffix);
list.addAll(_list);
} else {
String name = file.getName();
int idx = name.lastIndexOf(".");
String suffix = name.substring(idx + 1);
if (suffix.equals(srcSuffix)) {
list.add(file.getAbsolutePath());
}
}
}
return list;
}
public static void main(String[] args) {
// 指定的源路径
String srcPath = "E:\\workspace\\mysql";
// 提取的内容到目标文件
String targetPath = "F:\\999.txt";
List<String> list = QueryZh.listPath(new File(srcPath), "java");
for (String s : list) {
System.out.println(s);
QueryZh.search(s, targetPath);
}
QueryZh.mergeFile("F:\\999.txt");
}
}