一个使用比较全面的工具类(作用于抽取汉字,生成文件,其中有一些常用的方法)

package com.shine.eiuop.utils.Utils2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.shine.eiuop.utils.FileUtils;
import com.shine.framework.commutil.typewrap.EDto;

/**
* title: 清除注释
*
* @author Administrator
* @时间 2009-7-30:下午09:28:10
*/
public class FileCopyChineseUtils {

/** 根目录 */
public static String rootDir = "C:\\Users\\14423\\Desktop\\msp中文翻译\\msp_bak\\msp_bak\\msp";

public static StringBuilder finalAll = new StringBuilder();

public static void main(String args[]) throws Exception {
dofind(rootDir);
}

public static void dofind(String rootDir) throws Exception {
//列出文件
List<String> allFileStrings = new ArrayList<String>();
allFileStrings.add("/pages/transfer/apply/tawb/TransferTa.jsp");
String alltmSr = null;
for (int i = 0; i < allFileStrings.size(); i++) {
//遍历所有文件,找到指定的文件
alltmSr = deepDir(rootDir,allFileStrings.get(i).replaceAll("/", "\\\\"));
}

System.out.println(alltmSr);
String[] stringArrStrings = alltmSr.toString().split("\\r\\n");
String file_path = "D:\\SHINE_ROOT\\mspChinese.csv";
String file_name = "mspChinese.csv";
writeDataToCsvFile1(file_path,file_name,stringArrStrings);
finalAll.setLength(0);
}

/**
* 遍历所有文件,找到指定的jsp文件
* @param rootDir
* @param fileRoot
* @return
* @throws Exception
*/
public static String deepDir(String rootDir,String fileRoot) throws Exception {
String string = "";
File folder = new File(rootDir);
StringBuilder alltmSr = new StringBuilder();
if (folder.isDirectory()) {
String[] files = folder.list();

for (int i = 0; i < files.length; i++) {
File file = new File(folder, files[i]);
if (file.isDirectory() && file.isHidden() == false) {
alltmSr.append(deepDir(file.getPath(),fileRoot));
} else if (file.isFile()) {
//找到指定的jsp文件
if (file.getPath().endsWith(fileRoot)) {
//一行行遍历。找到标配的js文件,和include的所有jsp文件
alltmSr.append(writeComment(file.getPath(),fileRoot));
}
}
}
} else if (folder.isFile()) {
if (folder.getPath().endsWith(fileRoot)) {
alltmSr.append(writeComment(folder.getPath(),fileRoot));
}
}
return alltmSr.toString();
}

/**
* 一行行遍历。找到标配的js文件,和include的所有jsp文件
* @param filePathAndName
* @param fileName
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static String writeComment(String filePathAndName,String fileName)
throws Exception {

StringBuilder tmSr = new StringBuilder();
if (filePathAndName.endsWith(".jsp")) {
tmSr.append(filePathAndName);
tmSr.append("\r\n"); // 添加换行符
//存放查出来的jsp文件
List<String> allFileStrings = new ArrayList<String>();
//存放自己的js文件,当做标配
List<String> allJsFile = new ArrayList<String>();
allJsFile.add(fileName.split("\\.")[0]+".js");

StringBuffer buffer = new StringBuffer();
String line = null; // 用来保存每行读取的内容
InputStream is = new FileInputStream(filePathAndName);
BufferedReader reader = null;
if (filePathAndName.endsWith(".java")) {
reader = new BufferedReader(new InputStreamReader(is,"GBK"));
}else {
reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
}

try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\r\n"); // 添加换行符
try {
line = reader.readLine();
String mane = "";
String finalName = "";
String foldFileString = "C:\\Users\\14423\\Desktop\\亚强\\msp中文翻译\\msp_bak\\msp_bak\\msp\\WebRoot";//文件根目录
if ( line != null && line.contains(".jsp\"")) {
if (line.indexOf("/") == line.lastIndexOf("/")) {// <jsp:include page="TaOtherFee.jsp"/>
//C:\Users\14423\Desktop\亚强\msp中文翻译\msp_bak\msp_bak\msp\WebRoot\pages\transfer\apply\instr
mane = (line.substring(line.indexOf("\"")+1,line.lastIndexOf("\""))).trim();
finalName = filePathAndName.substring(0,filePathAndName.lastIndexOf("\\")+1)+mane.split("/")[mane.split("/").length-1];
allFileStrings.add(finalName);
}else {// <jsp:include page="/pages/bizDesc.jsp"/>
mane = line.substring(line.indexOf("\"")+1,line.lastIndexOf("\"")).replace("/", "\\");
finalName = foldFileString + mane;
allFileStrings.add(finalName);
}
}
} catch (IOException e) {
e.printStackTrace();
} // 读取下一行
}
buffer.append("\r\n"); // 添加换行符
String filecontent = buffer.toString();
tmSr.append(deepMatcher(filecontent));

finalAll.append(tmSr.toString());

//自己的js当做标配
allJsFile = removeDuplicate(allJsFile);
for (int i = 0; i < allJsFile.size(); i++) {
deepDir(rootDir,allJsFile.get(i));
}

allFileStrings = removeDuplicate(allFileStrings);
for (int i = 0; i < allFileStrings.size(); i++) {
deepDir(rootDir,allFileStrings.get(i));
}

}else if (filePathAndName.endsWith(".js")) {
tmSr.append(filePathAndName);
tmSr.append("\r\n"); // 添加换行符
//存放查出来的ServiceImpl文件
List<String> allServiceFileStrings = new ArrayList<String>();
StringBuffer buffer = new StringBuffer();
String line = null; // 用来保存每行读取的内容
InputStream is = new FileInputStream(filePathAndName);
BufferedReader reader = null;
if (filePathAndName.endsWith(".java")) {
reader = new BufferedReader(new InputStreamReader(is,"GBK"));
}else {
reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
}
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\r\n"); // 添加换行符
try {
line = reader.readLine();
String mane = "";
if ( line != null && line.matches("(.*)Service.(.*)")) {
// pb.im.send.Methods = {
// QUERY_DETAIL:'InfoSendService.getMsgInfoDetailDTOByInfoId',
// QUERY_DICT_NOTE_LIST : "PubService.getDictNoteDropList", // 获取数据字典
//
// };
//这边会有上面两种情况;返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
if (line.indexOf("\'")==-1) {//不是单引号.是双引号
mane = (line.substring(line.indexOf("\"")+1,line.lastIndexOf("."))).trim();
allServiceFileStrings.add(mane+"Impl.java");
}else {//不是双引号.是单引号 QUERY_BANK_ACC_LIST : "TransferApplyTawbPageService
mane = (line.substring(line.indexOf("\'")+1,line.lastIndexOf("."))).trim();
allServiceFileStrings.add(mane+"Impl.java");
}
}
}catch (IOException e) {
e.printStackTrace();
} // 读取下一行
}
buffer.append("\r\n"); // 添加换行符
String filecontent = buffer.toString();
tmSr.append(deepMatcher(filecontent));
finalAll.append(tmSr.toString());

//遍历每一个涉及到的server.
allServiceFileStrings = removeDuplicate(allServiceFileStrings);
for (int i = 0; i < allServiceFileStrings.size(); i++) {
deepDir(rootDir,allServiceFileStrings.get(i));
}
}else if (filePathAndName.endsWith(".java")) {
tmSr.append(filePathAndName);
tmSr.append("\r\n"); // 添加换行符
StringBuffer buffer = new StringBuffer();
String line = null; // 用来保存每行读取的内容
InputStream is = new FileInputStream(filePathAndName);
BufferedReader reader = null;
if (filePathAndName.endsWith(".java")) {
reader = new BufferedReader(new InputStreamReader(is,"GBK"));
}else {
reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
}
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\r\n"); // 添加换行符
try {
line = reader.readLine();
}catch (IOException e) {
e.printStackTrace();
} // 读取下一行
}
buffer.append("\r\n"); // 添加换行符
String filecontent = buffer.toString();
tmSr.append(deepMatcher(filecontent));
finalAll.append(tmSr.toString());
}
return finalAll.toString();
}


public static String deepMatcher(String filecontent) throws Exception {
StringBuilder tmSr = new StringBuilder();
String regex = "[\u4e00-\u9fa5]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(filecontent);

//如果文件没有汉字,则文件路径不要出来
// if (matcher.find()!=false) {
// tmSr.append(filePathAndName);
// tmSr.append("\r\n"); // 添加换行符
// }

int tmp = -1;

while (matcher.find()) {
MatchResult result = matcher.toMatchResult();
int start = result.start();
int end = result.end();
if(tmp == start || tmp == -1) {
// 判断连续
tmSr.append(filecontent.substring(start, end));
}else {
// 不连续
tmSr.append("\r\n");
tmSr.append(filecontent.substring(start, end));
}
tmp = end;
}
tmSr.append("\r\n"); // 添加换行符
return tmSr.toString();
}

/**
*
* @Description 写csv文件,
* @param filePath
* @param fields
* @param dtos void
* @param
* @throws @author CYQ
* @date 2019年11月18日 上午9:45:31
* @see
*/
public static void writeDataToCsvFile1(String filePath, String fileName,String[] datas) throws Exception {

File csvFile = null;
BufferedWriter csvFileOutputStream = null;
FileOutputStream fos = null;
String uuidFilePath = "D:\\SHINE_ROOT\\mspChinese.csv";
try {
FileUtils.createNewFile(filePath);
FileUtils.createNewFile(uuidFilePath);
csvFile = new File(filePath);
try {
// 如果文件不存在,则创建新的文件
if (!csvFile.exists()) {
csvFile.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
// 写入bom头
byte[] uft8bom = { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
fos = new FileOutputStream(csvFile);
//fos.write(uft8bom);

// UTF-8使正确读取分隔符","
// 如果生产文件乱码,windows下用gbk,linux用UTF-8
//csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"), 1024);

//csvFileOutputStream.newLine();
for (String dto : datas) {
if ("".equals(dto)!=true) {
fos.write((dto+"\r\n").getBytes());
}
}
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 去重
* @param list
* @return
*/
public static List<String> removeDuplicate(List<String> list){
List<String> listTemp = new ArrayList<String>();
for(int i=0;i<list.size();i++){
if(!listTemp.contains(list.get(i))){
listTemp.add(list.get(i));
}
}
return listTemp;
}
}

posted @ 2020-07-23 11:31  rearboal  阅读(161)  评论(0编辑  收藏  举报