package com.bushangbuxia.wxtoqq.croe;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author 火星来客
* @since 2019-07-15
*/
public class WxToQqUtils {
static String sourceDirectory = "F:\\wx-miniapp";
static String targetDirectory = "D:\\qq-miniapp";
public static final int EOF = -1;
private static Map<String, String> suffixMap = new HashMap<String, String>() {
private static final long serialVersionUID = 4164251304476781073L;
{
put(".wxss", ".qss");
put(".wxml", ".qml");
}
};
public static void main(String[] args) {
try {
WxToQqUtils.convert();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void convert() throws IOException {
File file = new File(sourceDirectory);
if(!file.isDirectory()) {
return ;
}
convertFile(file);
}
private static void convertFile(File file) throws IOException {
File[] fs = file.listFiles();
for(File f:fs){
if(f.isDirectory()) {
convertFile(f);
}else {
createTargetFile(f);
}
}
}
private static void createTargetFile(File sourceFile) throws IOException {
String parent = sourceFile.getParent();
String targetParent = parent.replace(sourceDirectory, targetDirectory);
String fileContent = null;
String targetFileName = getTargetFileName(sourceFile.getName());
File targetFile = new File(targetParent+"/"+targetFileName);
if(!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
if(targetFileName.endsWith(".js")) {
fileContent = getFileContent(sourceFile,"wx\\.","qq.");
writeContent(targetFile, fileContent);
}else if(targetFileName.endsWith(".wxss")) {
fileContent = getFileContent(sourceFile,".wxss",".qss");
writeContent(targetFile, fileContent);
}else if(targetFileName.endsWith(".wxml")) {
fileContent = getFileContent(sourceFile,".wxml",".qml");
writeContent(targetFile, fileContent);
}else {
copy(sourceFile, targetFile);
}
}
private static void copy(File sourceFile,File targetFile) throws IOException {
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (EOF != (n = inBuff.read(buffer))) {
outBuff.write(buffer, 0, n);
}
outBuff.flush();
inBuff.close();
outBuff.close();
output.close();
input.close();
}
private static void writeContent(File file,String content) throws IOException {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(content);
fileWriter.flush();
fileWriter.close();
}
private static String getTargetFileName(String fileName) {
if(fileName.indexOf(".")<=0) {
return fileName;
}
String prefix = fileName.substring(0,fileName.indexOf("."));
String suffix = fileName.substring(fileName.indexOf("."));
String targetSuffix = suffixMap.get(suffix);
if(targetSuffix == null) {
targetSuffix = suffix;
}
String targetFileName = prefix+targetSuffix;
return targetFileName;
}
private static String getFileContent(final File file, String s, String t) throws IOException {
try (FileReader reader = new FileReader(file); BufferedReader br = new BufferedReader(reader)) {
StringBuffer buffer = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
buffer.append(line).append("\n");
}
String temp = buffer.toString();
if (s != null && t != null) {
temp = temp.replaceAll(s, t);
}
return temp;
} catch (IOException e) {
throw e;
}
}
}

九幽天决
- 粉丝: 11
最新资源
- 机械CADCAM技术第章.pptx
- 我国网络零售业发展战略研究讲解.doc
- 各种SAR成像算法总结-推荐文档.pdf
- 电子政务整体解决方案——全面构建政府电子信息化工作平台.docx
- 会计信息化发展问题探讨【会计实务操作教程】.pptx
- 电子商务实习简历.docx
- 神经网络感知器.ppt
- 直线段裁剪算法省名师优质课赛课获奖课件市赛课百校联赛优质课一等奖课件.ppt
- 基于单片机住宅防火防盗报警系统毕业设计.docx
- 企业可观测性实施指南
- 游戏开发制作流程分工和薪酬揭秘.doc
- C++图书馆管理系统毕业设计(含源文件).doc
- 网络营销模拟试题2套期末考试卷-AB卷-期末测试卷模拟卷测试题带答案-(1).doc
- 中小学校舍信息管理系统网络版试点方案.doc
- 国际工程项目管理总结.docx
- 机舱软件调试方案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


