摘自:https://www.oschina.net/code/snippet_87799_1612

Native2Ascii文件转换 -- 待完善

 package com.xxx.xxx.Util;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; /**
* 类、接口等说明
*
* @Date
* @version v1.0
* @author   
*/
public class ConvertFileNative2ASCIIUtil { public static boolean native2Ascii(String srcFileName, String srcFilecharsetName, String dstFileName,
String dstFilecharsetName) throws IOException { File srcFileNameFile = new File(srcFileName); if (!srcFileNameFile.exists()) {
throw new IOException(" File(" + srcFileName + ") Parameter does not reference a file that exists");
} else if (!srcFileNameFile.isFile()) {
throw new IOException("File(" + srcFileName + ") Parameter exists, but does not reference a file");
} else if (!srcFileNameFile.canRead()) {
throw new IOException("File(" + srcFileName + ") exists and is a file, but cannot be read.");
} else {
String content = readFile(srcFileName, srcFilecharsetName);
String ascii = Native2ASCIIUtil.native2Ascii(content);
writeFile(dstFileName, ascii, dstFilecharsetName);
} return true;
} public static boolean ascii2Native(String srcFileName, String srcFilecharsetName, String dstFileName,
String dstFilecharsetName) throws IOException { File srcFileNameFile = new File(srcFileName); if (!srcFileNameFile.exists()) {
throw new IOException(" File(" + srcFileName + ") Parameter does not reference a file that exists");
} else if (!srcFileNameFile.isFile()) {
throw new IOException("File(" + srcFileName + ") Parameter exists, but does not reference a file");
} else if (!srcFileNameFile.canRead()) {
throw new IOException("File(" + srcFileName + ") exists and is a file, but cannot be read.");
} else {
String content = readFile(srcFileName, srcFilecharsetName);
String nativeString = Native2ASCIIUtil.ascii2Native(content);
writeFile(dstFileName, nativeString, dstFilecharsetName);
} return true;
} public static void writeFile(String path, String content, String encoding) throws IOException {
File file = new File(path);
file.delete();
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
writer.write(content);
writer.close();
} public static String readFile(String path, String encoding) throws IOException {
String content = "";
File file = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
String line = null;
while ((line = reader.readLine()) != null) {
content += line + "\n";
}
reader.close();
return content;
}
}
 package com.xxx.xxx.Util;

 /**
* 类、接口等说明
*
* @Date
* @version v1.0
* @author
*/
public class Native2ASCIIUtil { /**
* prefix of ascii string of native character
*/
private static String PREFIX = "\\u"; /**
* Native to ascii string. It's same as execut native2ascii.exe.
* @param str native string
* @return ascii string
*/
public static String native2Ascii(String str) {
char[] chars = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
sb.append(char2Ascii(chars[i]));
}
return sb.toString();
} /**
* Native character to ascii string.
* @param c native character
* @return ascii string
*/
private static String char2Ascii(char c) {
if (c > 255) {
StringBuilder sb = new StringBuilder();
sb.append(PREFIX);
int code = (c >> 8);
String tmp = Integer.toHexString(code);
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
code = (c & 0xFF);
tmp = Integer.toHexString(code);
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
return sb.toString();
} else {
return Character.toString(c);
}
} /**
* Ascii to native string. It's same as execut native2ascii.exe -reverse.
* @param str ascii string
* @return native string
*/
public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf(PREFIX);
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf(PREFIX, begin);
}
sb.append(str.substring(begin));
return sb.toString();
} /**
* Ascii to native character.
* @param str ascii string
* @return native character
*/
private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException("Ascii string of a native character must be 6 character.");
}
if (!PREFIX.equals(str.substring(0, 2))) {
throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\".");
}
String tmp = str.substring(2, 4);
int code = Integer.parseInt(tmp, 16) << 8;
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16);
return (char) code;
}
}

最新文章

  1. EntityFrame CodeFirst 自动生成表
  2. win7+vs2010+opencv2.4.6配置
  3. 【URAL 1486】Equal Squares
  4. jQuery知识点总结(第二天)
  5. nmap常用命令
  6. Unity C#和OC互相调用
  7. [ES6] 16. Object Enhancements
  8. Pair of Numbers
  9. asp.net mvc放在iis7.5中提示404错误 js异步请求失效解决办法
  10. NPOI封装
  11. C#中鼠标划过按钮时候的提示信息
  12. Photoshop像素级画笔工具
  13. dm_analysis
  14. PAT甲题题解-1022. Digital Library (30)-map映射+vector
  15. python创建与遍历List二维列表
  16. Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression
  17. Maven镜像收集
  18. 吴裕雄 数据挖掘与分析案例实战(13)——GBDT模型的应用
  19. 团体程序设计天梯赛-练习集 L1-031. 到底是不是太胖了
  20. Linux操作系统CentOS7.2发行版本的安装与配置(安装是选择服务器类型)

热门文章

  1. python-错误
  2. FC 协议
  3. Java 面向对象 初探
  4. user
  5. 升级oracle 9i到10g
  6. Julia - 函数运算符
  7. 【转】Android 中处理崩溃异常并重启程序出现页面重叠的问题
  8. 第六章 声明式服务调用: Spring Cloud Feign
  9. windows兼容方式安装python[转]
  10. 6、数据类型四:sets