项目文件转码

package com.peachtao.file.op;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;

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.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

//此文件负责项目文件转码,项目文件需要使用到common-io.jar 因为涉及到文件操作的辅助类,需要使用到cpdetector_1.0.7.jar,chardet.jar,antlr.jar用作编码检测
public class CharEncodingExchange {

private final static String SOURCE_ENCODING = "GB2312";
private final static String TARGET_ENCODING = "UTF-8";
// 文件源目录
private static String SOURCE_DIR = "D:\\xampp\\htdocs\\uchome";
// 文件目标目录
private static String TARGET_DIR = "d:\\tem";
// 允许转码的文件后缀
private static String suffAllow = "html,htm,txt,js,php,sql,inc,tpl,css,java,jsp,xml,tld,properties";
private static String suffForbiden = "db";

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileUtils.deleteDirectory(new File(TARGET_DIR));
exchange(SOURCE_DIR);
} catch (Exception e) {
// TODO Auto-generated catch blockXj
e.printStackTrace();
}
System.out.println("----------end --------------");
}

/**
* exchange the character encoding from srcDir to targetDir
*
* @param srcDir
* @param targetDir
*/
public static void exchange(String srcDir) {
String absPath = "";
if (!srcDir.equals(SOURCE_DIR)) {
absPath = srcDir.substring(SOURCE_DIR.length());
String targetDir = TARGET_DIR + absPath;
File targetDirectory = new File(targetDir);
if (targetDirectory.isDirectory() && !targetDirectory.exists()) {
targetDirectory.mkdirs();
}
}

File sourceDirectory = new File(srcDir);
if (sourceDirectory.exists()) {
if (sourceDirectory.isFile()) {
String targetFilePath = TARGET_DIR + absPath;
// System.out.println(sourceDirectory.getAbsolutePath() + " "
// + targetFilePath);
String suff = absPath.substring(absPath.lastIndexOf(".") + 1);
System.out.println(sourceDirectory.getAbsolutePath()+"\t"+getFileCharacterEnding(sourceDirectory));
try {
if (!suffForbiden.equals(suff)) {
if (suffixAllow(suff)&&!"UTF-8".equals(getFileCharacterEnding(sourceDirectory))) {
fileEncodingExchange(sourceDirectory,
targetFilePath);
} else {
fileCopy(sourceDirectory, targetFilePath);
// System.out.println(sourceDirectory);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
File[] childs = sourceDirectory.listFiles();

for (File child : childs)
exchange(child.getPath());
}
}
}

private static void fileEncodingExchange(File infile,
String targetAbsFilePath) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
FileChannel fcin = null;
FileChannel fcout = null;

String tmpTargetPath = targetAbsFilePath.substring(0, targetAbsFilePath
.lastIndexOf(File.separator));
File tmpTargetDir = new File(tmpTargetPath);
if (!tmpTargetDir.exists())
tmpTargetDir.mkdirs();
try {
fin = new FileInputStream(infile);
fout = new FileOutputStream(targetAbsFilePath);
fcin = fin.getChannel();
fcout = fout.getChannel();

ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (true) {
buffer.clear();
int r = fcin.read(buffer);
if (r == -1) {
break;
}
buffer.flip();
String encoding = System.getProperty("file.encoding");
//检测原理,将文件按encoding解码后在编码成TARGET_ENCODING
fcout.write(ByteBuffer.wrap(Charset.forName(encoding).decode(
buffer).toString().getBytes(TARGET_ENCODING)));
// fcout.write(buffer);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fin != null) {
fin.close();
}
if (fcin != null) {
fcin.close();
}
if (fout != null)
fout.close();
if (fcout != null)
fcout.close();
}
}

private static void fileCopy(File infile, String targetAbsFilePath) {
try {
FileUtils.copyFile(infile, new File(targetAbsFilePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void fileEncodingExchange0(File infile,
String targetAbsFilePath) throws IOException {
FileInputStream fis = new FileInputStream(infile);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));

File targetFile = new File(targetAbsFilePath);
if (!targetFile.exists()) {
targetFile.mkdir();
}
FileOutputStream fos = new FileOutputStream(targetFile);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos,
TARGET_ENCODING));

int len = 1024;
char[] buf = new char[len];

int numRead;
while ((numRead = in.read(buf, 0, len)) != -1) {
out.write(buf, 0, numRead);
}
out.close();
in.close();
// System.out.println(infile+"\t"+targetAbsFilePath);
}

private static boolean suffixAllow(String suff) {
boolean flag = false;
String[] suffs = suffAllow.trim().split(",");
for (String a : suffs) {
if (suff.endsWith(a)) {
flag = true;
break;
}
}
return flag;
}

public static String getFileCharacterEnding(File file) {

String fileCharacterEnding = "UTF-8";

CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(JChardetFacade.getInstance());

Charset charset = null;

// File f = new File(filePath);

try {
charset = detector.detectCodepage(file.toURL());
} catch (Exception e) {
e.printStackTrace();
}
if (charset != null) {
fileCharacterEnding = charset.name();
}

return fileCharacterEnding;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值