摘自: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;
}
}

Native2Ascii文件转换 -- 待完善的更多相关文章

  1. C# 将多个office文件转换及合并为一个PDF文件

    PDF文件介绍 PDF(Portable Document Format )文件源于20世纪90年代初期,如今早已成为了一种最流行的的文件格式之一.因为PDF文件有很多优点: 支持跨平台和跨设备共享 ...

  2. mpp文件转换成jpg图片,可以用pdf文件做中转站

    用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了

  3. php将文件转换成二进制输出[转]

    header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fope ...

  4. ocx文件转换成C#程序引用的DLL

    将ocx文件转换成C#程序引用的DLL文件的办法  将ocx文件转换成C#程序引用的DLL文件的办法,需要的朋友可以参考一下  1.打开VS2008或VS2010命令提示符(此例用VS2008) 将o ...

  5. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...

  6. Python:将utf-8格式的文件转换成gbk格式的文件

    需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: def ReadFile(filePath,encoding="utf-8"): with codecs.ope ...

  7. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

  8. Marvel – 将图像和源文件转换成互动,共享的原型

    Marvel 是一款非常简单的工具,将图像和设计源文件转换成互动,共享的原型,无需任何编码.原型可以通过点击几下鼠标就创建出来,能工作在任何设备上的浏览器,包括移动设备,台式机.Marvel 的一个特 ...

  9. 文件转换神器Pandoc使用

    最近记录笔记,改用Markdown格式.但有时需要分享下笔记,对于不懂markdown格式的同学来说阅读感觉不是那么友好.因此就一直在寻找一款文件转换的软件,之前因为用markdownpad来编写,可 ...

随机推荐

  1. 黄聪:VS2010中“新建项目”却没有“解决方案”节点,如何调出来

    工具->选项->项目和解决方案 把"总是显示解决方案"打 √ 就ok 了

  2. soap,socket

    Socket 接口是访问 Internet 使用得最广泛的方法. 如果你有一台刚配好TCP/IP协议的主机,其IP地址是202.120.127.201, 此时在另一台主机或同一台主机上执行ftp 20 ...

  3. git 本地与远程分支冲突 解决

    git pull origin master git rebase origin/master git merge origin/master git rebase --continue git pu ...

  4. js与jQuery的区分

    Js与Jquery的区分

  5. python学习笔记(六):常用模块

    一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...

  6. PHP错误日志记录文件位置确定

    1.确定web服务器 ( IIS, APACHE, NGINX 等) 以哪一种方式支持PHP,通常是有下面2种方式 通过模块加载的方式, 适用于apache 通过 CGI/fastCGI 模式, 该模 ...

  7. Tkinter LabelFrame

       Tkinter LabelFrame: 在一个labelframe一个简单的容器构件.其主要目的是作为一个间隔或复杂的窗口布局容器. 在一个labelframe一个简单的容器构件.其主要目的是作 ...

  8. How To Install MongoDB on CentOS 6

    How To Install MongoDB on CentOS 6 Posted on January 21, 2014 by J. Mays | Updated: January 22, 2014 ...

  9. ubuntu下安装stm32开发环境

    在windowns下开发stm32刚开始学最烦的就是创建工程模板,都不知道为什么要那样设置,而且步骤繁多.现在我告诉大家一个好消息,在linux下配置stm32开发环境包括创建工程,使用JLink仿真 ...

  10. Python格式符说明

    格式化输出 例如我想输出 我的名字是xxxx 年龄是xxxx name = "Lucy"age = 17print("我的名字是%s,年龄是%d"%(name, ...