标签:des blog java os io 文件 for 2014
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 = filePath + File.separator + fileName; // filePath.substring(2) \u7528\u4E8E\u53D6\u51FA\u76D8\u7B26\u4F8B\u5982H:%test\u53BB\u9664\u540E\u4E3A test String targetDir = targetPath + File.separator + filePath.substring(3); String targetFile = targetDir + File.separator + fileName; file = new File(targetDir); if (!file.exists()) {// \u521B\u5EFA\u5728\u6E90\u6587\u4EF6\u76F8\u5BF9\u5E94\u7684\u8DEF\u5F84 file.mkdirs(); } // file = new File(targetFile); // if (file.isFile()) {// \u5982\u679C\u5B58\u5728\u6587\u4EF6\u5219\u66FF\u6362 // file.delete(); // } String srcFileEncoding = FileUtil .getEncodeString(fileNameAndPathSrc); final StringBuffer buf = new StringBuffer("native2ascii -encoding"); buf.append(" ").append(srcFileEncoding); buf.append(" ").append(fileNameAndPathSrc).append(" ") .append(targetFile); // String // type=FileTypeMap.getDefaultFileTypeMap().getContentType(targetFile); // System.out.println(type+" "+buf.toString()); // \u6267\u884C\u8F6C\u6362 Process p = null; try { p = run.exec(buf.toString()); p.waitFor();// \u6CA1\u6709\u6B21\u53E5\u8BDD\u4F1A\u9020\u6210\u963B\u585E } catch (Exception e) { throw new Exception(e); } System.out.println(srcFileEncoding + "***** " + p.exitValue() + "--" + buf.toString()); } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //String srcPath = "H:/txt"; String srcPath ="E:/JavaWeb/Tarena"; String targetPath = "H:/"; //toUnicode("H:/t.txt", "H:/t2est.txt"); toUnicode2(srcPath, targetPath, true); System.err.println("over"); } }
Native2asciiUtil 文本文件转UNICODE编码文件(支持UTF-8,Unicode,UTF-16BE,ANSI|ASCII,GBK),布布扣,bubuko.com
Native2asciiUtil 文本文件转UNICODE编码文件(支持UTF-8,Unicode,UTF-16BE,ANSI|ASCII,GBK)
标签:des blog java os io 文件 for 2014
原文地址:http://blog.csdn.net/ctllin/article/details/38459133