网上无意中看见的,亲测过十分好用,分享给大家,顺便留下来备忘。
背景
在工程中查找未被国际化的部分。
代码
Main.java 执行类
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
//输出文件路径
public static String outFile = “D:/tiqu.properties”;
//输入文件夹路径
public static String inFolder = “D:/sinosoft/prpins-one/component”;
public static String charset = “gbk”;
public static void main(String[] args) throws IOException {
PrintWriter output = new PrintWriter(new FileWriter(new File(outFile)));
Process process = new Process(output, charset);
process.readDir(inFolder);
output.close();
}
}
Process.java 控制类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Process {
public static Queue queue = new ConcurrentLinkedQueue();
private PrintWriter output = null;
private String charset = null;
public Process(PrintWriter output, String charset) {
this.output = output;
this.charset = charset;
}
public void readTxt(File tempFile) throws IOException {
System.out.println(“#” + tempFile.getName() + “\n”);
output.write(“#” + tempFile.getName() + “\n”);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(tempFile), charset));
String tempString = “”;
while ((tempString = reader.readLine()) != null) {
queue.add(new DataModel(NormalState.normal, reader, tempString, 0,
output));
while (queue.size() > 0) {
DataModel dm = queue.remove();
dm.getState().handle(dm.getReader(), dm.getLine(),
dm.getFrom(), dm.getOutput());
}
}
reader.close();
}
public void readDir(String folder) throws IOException {
File dir = new File(folder);
// PrintWriter output = new PrintWriter(new FileWriter(new
// File(outFile)));
if (dir.isDirectory()) {
System.out.println(“#Dir#” + dir.getName() + “\n”);
output.write(“#Dir#” + dir.getName() + “\n”);
String[] children = dir.list();
for (int i = 0; i
File tempFile = new File(dir, children[i]);
if (tempFile.isDirectory())
readDir(tempFile.getPath());
else {
readTxt(tempFile);
}
}
}
}
}
DataModel.java 数据类型类
import java.io.BufferedReader;
import java.io.PrintWriter;
public class DataModel {
private State state;
private BufferedReader reader;
private String line;
private int from;
private PrintWriter output;
public DataModel(State state, BufferedReader reader, String line, int from,
PrintWriter output) {
super();
this.state = state;
this.reader = reader;
this.line = line;
this.from = from;
this.output = output;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public BufferedReader getReader() {
return reader;
}
public void setReader(BufferedReader reader) {
this.reader = reader;
}
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public PrintWriter getOutput() {
return output;
}
public void setOutput(PrintWriter output) {
this.output = output;
}
}
State.java 抽象状态类
import java.io.BufferedReader;
import java.io.PrintWriter;
public abstract class State {
public abstract void handle(BufferedReader reader,String line,int from,PrintWriter output);
}
import java.io.BufferedReader;
import java.io.PrintWriter;
public class NormalState extends State {
public static final NormalState normal = new NormalState();
@Override
public void handle(BufferedReader reader, String line, int from,
PrintWriter output) {
// TODO Auto-generated method stub
for (int k = from; k
char now = line.charAt(k);
if ((now == ‘”‘ && k == 0)
|| (now == ‘”‘ && k > 0 && line.charAt(k – 1) != ‘\\’)) {
Process.queue.add(new DataModel(QuotState.quotState, reader,
line, k + 1, output));
break;
} else if (‘/’ == now && k
&& (‘/’ == line.charAt(k + 1))) {
break;// go back to process’s excute
} else if (‘/’ == now && k
&& (‘*’ == line.charAt(k + 1))) {
Process.queue.add(new DataModel(BlockNote.blockNote, reader,
line, k + 2, output));
break;
}
}
}
}
import java.io.BufferedReader;
import java.io.PrintWriter;
public class LineNote extends State{
@Override
public void handle(BufferedReader reader, String line, int from,PrintWriter output) {
// TODO Auto-generated method stub
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
class BlockNote extends State {
public static final BlockNote blockNote = new BlockNote();
@Override
public void handle(BufferedReader reader, String line, int from,
PrintWriter output) {
// TODO Auto-generated method stub
boolean out = false;
int k = from;
for (k = from; k
char now = line.charAt(k);
if (‘*’ == now && k
&& (‘/’ == line.charAt(k + 1))) {
out = true;
break;
}
}
try {
label:
while (!out && (line = reader.readLine()) != null) {
for (k = 0; k
char now2 = line.charAt(k);
if (‘*’ == now2 && k
&& (‘/’ == line.charAt(k + 1))) {
out = true;
break label;
}
}
}
if (out) {
Process.queue.add(new DataModel(NormalState.normal, reader,
line, k + 2, output));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.PrintWriter;
public class QuotState extends State {
public static final QuotState quotState = new QuotState();
public static int keynum = 0;
@Override
public void handle(BufferedReader reader, String line, int from,
PrintWriter output) {
// TODO Auto-generated method stub
int beginIndex = from, endIndex = from, k = from;
boolean out = false;
for (k = from; k
char now = line.charAt(k);
if ((now == ‘”‘ && k == 0)
|| (now == ‘”‘ && k > 0 && line.charAt(k – 1) != ‘\\’)) {
Process.queue.add(new DataModel(NormalState.normal, reader,
line, k + 1, output));
break;
} else if (line.substring(k, k + 1).getBytes().length > 1) {
for (; k
if (line.charAt(k) == ‘”‘ && line.charAt(k – 1) != ‘\\’) {
endIndex = k;
break;
}
}
System.out.println(“key: ” + keynum++ + ” ”
+ line.substring(beginIndex, endIndex));
output.write(line.substring(beginIndex, endIndex) + “\n”);
out = true;
break;
}
}
if (out) {
Process.queue.add(new DataModel(NormalState.normal, reader, line,
k + 1, output));
}
}
}