}
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
public class CompareFiles {
static Set
/**
* 比较指定的7个文件
*
*/
private static final String[] fileNames = new String[] { "cec_strings.xml",
"menu_strings.xml", "mmp_strings.xml", "nav_strings.xml",
"strings.xml", "thr_menu_strings.xml", "timeshift_strings.xml" };
/**
*
* @param englishPath
* 英文目录的地址
* @param otherLanguagePath
* 其它语言的地址
* @return 结果保存在指定的文件文件中
*/
public static void compareFile(String englishPath,
String otherLanguagePath, String output, String translateOutput) {
File[] englishFiles = FileUtils.getFiles(englishPath);
File[] otherLanguageFiles = FileUtils.getFiles(otherLanguagePath);
for (String fileName : fileNames) {
File englishFile = FileUtils.getFileByFileName(englishFiles,
fileName);
File otherLanguageFile = FileUtils.getFileByFileName(
otherLanguageFiles, fileName);
Map
Map
.parseXml(otherLanguageFile);
Set
Set
// 过滤出englishKey特有的key
SetView
otherLanguageKey);
// 过滤出englishKey和otherLanguageKey的交集
SetView
otherLanguageKey);
Map
english);
Map
english, otherLanguage);
Set
Set
res.addAll(englishResValue);
res.addAll(otherLanguageResValue);
FileUtils.writerHashMapToFile(englishRes, output + File.separator
+ fileName.substring(0, fileName.lastIndexOf(".xml")));
FileUtils.writerHashMapToFile(
otherLanguageRes,
output
+ File.separator
+ fileName.substring(0,
fileName.lastIndexOf(".xml")));
}
ExcelUtils.createFixationSheet(res, translateOutput + File.separator
+ System.currentTimeMillis() + ".xls");
res.clear();
}
public static void printViewSet(SetView
Iterator iterator = views.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
/**
* 将map中的value转换成集合
*
* @param originalMap
* @return
*/
public static Set
Set
Iterator
while (iterator.hasNext()) {
String value = iterator.next();
values.add(originalMap.get(value));
}
return values;
}
/**
* 如果key相同但是value不同,则构成新的集合
*
* @param views
* @param english
* @param otherLanguage
* @return
*/
public static Map
SetView
Map
Map
Iterator
while (iterator.hasNext()) {
String value = iterator.next();
if (english.get(value).equals(otherLanguage.get(value))) {
res.put(value, english.get(value));
}
}
return res;
}
/**
* 将原始map中的特有的key过滤到新的集合中
*
* @param views
* 过滤出来的集合
* @param originalMap
* 原始map集合
* @return
*/
public static Map
SetView
Map
Iterator
while (iterator.hasNext()) {
String value = iterator.next();
res.put(value, originalMap.get(value));
}
return res;
}
/**
* 比较公司标准与外国语言的差集
*
* @param excelFirstPath
* @param excelSecondPath
* @param excelOutputPath
*/
public static void intersectTwoExcel(String excelFilePath,
String excelDirPath, String excelOutputPath) {
Set
Set
englishComany.addAll(ExcelUtils.readExcel(excelFilePath));
otherLanguageValues.addAll(ExcelUtils.readExcel(excelDirPath));
SetView
englishComany);
SetView
otherLanguageValues);
ExcelUtils.createFixationSheet(differenec, excelOutputPath + File.separator
+ "差集"+System.currentTimeMillis() + ".xls");
ExcelUtils.createFixationSheet(union, excelOutputPath + File.separator
+ "交集"+System.currentTimeMillis() + ".xls");
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
/**
* Excel工具类
*
* @author yy
*
*/
public class ExcelUtils {
/**
*
* @param res
* 内存中的数据
* @param path
* 要输出的excel文件地址
*/
public static void createFixationSheet(Set
File outputfile = new File(path);
if (!outputfile.exists()) {
try {
outputfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream os = null;
try {
os = new FileOutputStream(outputfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List
values.addAll(res);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
sheet.createFreezePane(0, 1);
try {
HSSFRow row = null;
for (int i = 0; i < values.size(); i++) {
row = sheet.createRow((short) i);
cteateCell(wb, row, (short) 0, values.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
try {
wb.write(os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.flush();
if (os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建excel单元格
*
* @param wb
* @param row
* @param col
* @param val
*/
private static void cteateCell(HSSFWorkbook wb, HSSFRow row, short col,
String val) {
HSSFCell cell = row.createCell(col);
cell.setCellValue(val);
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
cell.setCellStyle(cellstyle);
}
public static Set
Set
if (file == null) {
return res;
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = null;
sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
for (int i = 0; i <= rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row.getCell((short) 0) != null) {
row.getCell((short) 0).setCellType(Cell.CELL_TYPE_STRING);
res.add(row.getCell((short) 0).getStringCellValue());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
/**
* 过滤第一列和第二列都不为null,只要第一列
* @param file
* @return
*/
public static Set
Set
if (file == null) {
return res;
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = null;
sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
for (int i = 0; i <= rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row.getCell((short) 0) != null
&& row.getCell((short) 1) != null) {
row.getCell((short) 0).setCellType(Cell.CELL_TYPE_STRING);
res.add(row.getCell((short) 0).getStringCellValue());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public static Set
if (StringUtils.isBlank(path)) {
return new HashSet
}
File file = new File(path);
return readExcel(file);
}
public static void main(String[] args) {
Set
bb = readExcelFilterBlank(new File("C://Users/yy/Desktop/res/aa.xls"));
for (String a : bb) {
System.out.println(a);
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
/**
*
* 文件工具类
*/
public class FileUtils {
/**
* 获取指定路径下的所有文件
*
* @param path
* @return
*/
public static File[] getFiles(String path) {
File[] res = null;
if (StringUtils.isNotBlank(path)) {
File dictory = new File(path);
if (dictory.isDirectory()) {
res = dictory.listFiles();
}
}
return res;
}
/**
* 根据文件名获取指定的文件
* @param files 文件集合
* @param fileName 文件名
* @return
*/
public static File getFileByFileName(File[] files, String fileName) {
if (files != null && StringUtils.isNotBlank(fileName)) {
for (File file : files) {
if (file.getName().equals(fileName)) {
return file;
}
}
}
return null;
}
/**
* 将set中的值输出到指定的文件中
* @param values
* @param path
*/
public static void writerSetToFile(Set
File writeToFile=new File(path);
if(!writeToFile.exists()){
try {
writeToFile.createNewFile();
} catch (IOException e) {
}
}
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path);
bw = new BufferedWriter(fw);
for (String value : values) {
bw.write(value);
bw.newLine();
}
} catch (IOException e) {
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
}
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
}
}
}
/**
* 将map中的值输出到指定的文件中
* @param map
* @param path
*/
public static void writerHashMapToFile(Map
File writeToFile=new File(path);
if(!writeToFile.exists()){
try {
writeToFile.createNewFile();
} catch (IOException e) {
}
}
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path,true);
bw = new BufferedWriter(fw);
Set
for (String key : keys) {
bw.write(key + "=" + map.get(key));
bw.newLine();
}
bw.write("----------------------------------");
bw.newLine();
bw.newLine();
bw.newLine();
bw.newLine();
} catch (IOException e) {
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
}
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
}
}
}
}
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
*
* 解析xml文件例如
* when TalkBack On
*/
public class XMLParser {
/**
* 将指定的xml文件转换成Map key=app_name value=Live TV
*
* @param path
* @return
*/
public static Map
File file = new File(path);
return parseXml(file);
}
/**
* 将指定的xml文件转换成Map key=app_name value=Live TV
*
* @param path
* @return
*/
public static Map
Map
if (file == null) {
return attributes;
}
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(file);
} catch (DocumentException e) {
}
Element root = document.getRootElement();
// 遍历
listNodes(root, attributes);
return attributes;
}
/**
* 递归遍历xml的节点,并解析成key-value的内存结构
*
* @param node
* 当前节点
* @param attributes
* 内存中数据结构
*/
private static void listNodes(Element node, Map
// 首先获取当前节点的所有属性节点
List
// 遍历属性节点
for (Attribute attribute : list) {
if ("string".equals(node.getName())) {
attributes.put(attribute.getValue(), node.getText());
}
}
// 同时迭代当前节点下面的所有子节点
// 使用递归
Iterator
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e, attributes);
}
}
/**
* 循环遍历map,用于测试xml解析是否正确
*
* @param attributes
*/
public static void printMap(Map
Set
for (String key : keys) {
System.out.println(key + ":" + attributes.get(key));
}
}
}