Java 中文內容提取


網上無心中看見的,親測過十分好用,分享給你們,順便留下來備忘。java

背景

在工程中查找未被國際化的部分。ide

代碼

Main.java 執行類this

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 控制類code

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<DataModel> queue = new ConcurrentLinkedQueue<DataModel>();    
 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 < children.length; i++) {    
 File tempFile = new File(dir, children[i]);    
 if (tempFile.isDirectory())    
 readDir(tempFile.getPath());    
 else {    
 readTxt(tempFile);    
 }    
 }    
 }    
 }    
}



DataModel.java 數據類型類component

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 抽象狀態類orm

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 < line.length(); 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.length() - 1    
 && ('/' == line.charAt(k + 1))) {    
 break;// go back to process's excute    
 } else if ('/' == now && k < line.length() - 1    
 && ('*' == 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 < line.length(); k++) {    
 char now = line.charAt(k);    
 if ('*' == now && k < line.length() - 1    
 && ('/' == line.charAt(k + 1))) {    
 out = true;    
 break;    
 }    
 }
 try {   
 label:    
 while (!out && (line = reader.readLine()) != null) {    
 for (k = 0; k < line.length(); k++) {    
 char now2 = line.charAt(k);    
 if ('*' == now2 && k < line.length() - 1    
 && ('/' == 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 < line.length(); 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 < line.length(); 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));    
 }
 }
}
相關文章
相關標籤/搜索