首页 > 代码库 > Native2asciiUtil 文本文件转UNICODE编码文件(支持UTF-8,Unicode,UTF-16BE,ANSI|ASCII,GBK)

Native2asciiUtil 文本文件转UNICODE编码文件(支持UTF-8,Unicode,UTF-16BE,ANSI|ASCII,GBK)

package com.ctl.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @author Administrator
 * @Description \u5C06\u6587\u672C\u6587\u4EF6\u8F6C\u6362\u4E3AUNICODE\u7F16\u7801
 */
public class Native2asciiUtil {
	/**
	 * @description \u5C06\u6587\u672C\u6587\u4EF6\u8F6C\u6362\u6210UNICODE\u7F16\u7801\u652F\u6301UTF-8,Unicode,UTF-16BE,ANSI|ASCII,GBK
	 * @param srcFile
	 *            \u6E90\u6587\u4EF6\u8DEF\u5F84 \u4F8B\u5982C:/test/test.txt
	 * @param targetFilePath
	 *            \u76EE\u6807\u8DEF\u5F84 \u8F6C\u8FC7\u540E\u7684\u76EE\u6807\u6587\u4EF6H:/zz/test.java
	 * @return
	 * @throws Exception
	 */
	public static boolean toUnicode(String srcFile, String targetFilePath)
			throws Exception {
		File file = new File(targetFilePath);
		String path = file.getParent();
		File file2 = new File(path);
		file2.mkdirs();
		String srcFileEncoding = FileUtil.getEncodeString(srcFile);
		StringBuffer buf = new StringBuffer("native2ascii -encoding");
		buf.append(" ").append(srcFileEncoding).append(" ").append(srcFile)
				.append(" ").append(targetFilePath);
		try {
			Runtime run = Runtime.getRuntime();
			run.exec(buf.toString());
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * @description 
	 *              \u5C06\u6307\u5B9A\u6587\u4EF6\u5939\u7684\u6587\u672C\u6587\u4EF6\u8F6C\u6362\u6210unicode\u7F16\u7801\u9ED8\u8BA4\u5305\u542B\u5B50\u6587\u4EF6\u5939\u652F\u6301UTF-8,Unicode,UTF-16BE,ANSI|
	 *              ASCII,GBK
	 * @param srcPath
	 *            \u539F\u76EE\u5F55
	 * @param targetPath
	 *            \u76EE\u6807\u76EE\u5F55
	 * @throws Exception
	 */
	public static void toUnicode2(String srcPath, String targetPath)
			throws Exception {
		toUnicode2(srcPath, targetPath, true);
	}

	/**
	 * @description 
	 *              \u5C06\u6307\u5B9A\u6587\u4EF6\u5939\u7684\u6587\u672C\u6587\u4EF6\u8F6C\u6362\u6210unicode\u7F16\u7801\u9ED8\u8BA4\u5305\u542B\u5B50\u6587\u4EF6\u5939\u652F\u6301UTF-8,Unicode,UTF-16BE,ANSI|
	 *              ASCII,GBK
	 * @param srcPath
	 *            \u539F\u76EE\u5F55
	 * @param targetPath
	 *            \u76EE\u6807\u76EE\u5F55
	 * @param childDir
	 *            \u662F\u5426\u5305\u542B\u5B50\u6587\u4EF6\u5939
	 * @throws Exception
	 */
	public static void toUnicode2(String srcPath, String targetPath,
			boolean childDir) throws Exception {
		// map\u4E2D\u5B58\u653E\u6240\u6709\u7684\u6587\u4EF6\u540D \u548C\u6587\u4EF6\u8DEF\u5F84

		final Runtime run = Runtime.getRuntime();
		Map<String, String> map = new HashMap<String, String>();
		map = FileUtil.getAllFileNameAndFilePathMap(srcPath, childDir, map);
		Set<String> key = map.keySet();
		Iterator<String> it = key.iterator();

		File file = new File(targetPath);
		// \u521B\u5EFA\u76EE\u6807\u8DEF\u5F84
		file.mkdirs();
		while (it.hasNext()) {
			String fileName = it.next();
			String filePath = map.get(fileName);
			String fileNameAndPathSrc = http://www.mamicode.com/filePath + File.separator + fileName;>