Native2Ascii文件转换 -- 待完善
摘自: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文件转换 -- 待完善的更多相关文章
- C# 将多个office文件转换及合并为一个PDF文件
PDF文件介绍 PDF(Portable Document Format )文件源于20世纪90年代初期,如今早已成为了一种最流行的的文件格式之一.因为PDF文件有很多优点: 支持跨平台和跨设备共享 ...
- mpp文件转换成jpg图片,可以用pdf文件做中转站
用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了
- php将文件转换成二进制输出[转]
header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fope ...
- ocx文件转换成C#程序引用的DLL
将ocx文件转换成C#程序引用的DLL文件的办法 将ocx文件转换成C#程序引用的DLL文件的办法,需要的朋友可以参考一下 1.打开VS2008或VS2010命令提示符(此例用VS2008) 将o ...
- nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件
目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...
- Python:将utf-8格式的文件转换成gbk格式的文件
需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: def ReadFile(filePath,encoding="utf-8"): with codecs.ope ...
- 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件
PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...
- Marvel – 将图像和源文件转换成互动,共享的原型
Marvel 是一款非常简单的工具,将图像和设计源文件转换成互动,共享的原型,无需任何编码.原型可以通过点击几下鼠标就创建出来,能工作在任何设备上的浏览器,包括移动设备,台式机.Marvel 的一个特 ...
- 文件转换神器Pandoc使用
最近记录笔记,改用Markdown格式.但有时需要分享下笔记,对于不懂markdown格式的同学来说阅读感觉不是那么友好.因此就一直在寻找一款文件转换的软件,之前因为用markdownpad来编写,可 ...
随机推荐
- java代码----I/O流从控制台输入信息判断并抛出异常
package com.a.b; import java.io.*; public class Yu { public static void main(String[] args) throws I ...
- Timesten 日常管理命令合集
Timesten 日常管理命令合集 以下所有操作都是基于TT 11 版,早前版本本人没用过,命令是否适用我不清楚啊! 各类服务管理 一.TT的启停 停服务: 1.停止复制与cache 进程: ...
- 关于i++的解释
根据 JavaScript Language Specification 的定义, 表达式 A=B的执行步骤,简单地说,就是:1. 对等号左边的表达式 A 进行计算,算出最终的结果,必须是参照或变量: ...
- Web端优秀图表控件
百度echart http://echarts.coding.io/doc/example.html C#+JQuery+.Ashx+百度Echarts 实现全国省市地图和饼状图动态数据图形报表的统计 ...
- 关于Spring的Quartz的xml配置的例子
<span style="font-size:16px"></span><h3><span style="font-family ...
- 【转】Android 中处理崩溃异常并重启程序出现页面重叠的问题
原文地址:http://blog.csdn.net/jiang547860818/article/details/53641113 android开发中经常会遇到程序异常,而已常常会遇到一出现异常AP ...
- 不同的类UNIX操作系统密码破解方法介绍
(一)Linux 系统密码破解 1.在grub选项菜单按E进入编辑模式 2.编辑kernel那行 /init 1 (或/single) 3.按B重启 4.进入后执行下列命令 root@#passwd ...
- 控件的WndProc WindowProc
SubClassWndProc This example shows how to use the WndProc method and the WindowProc property to subc ...
- GetEnumName 枚举名称 字符串
System.TypInfo.pas System.TypInfo.hpp http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TypIn ...
- Spring与Redis的实现
前言 Redis作为缓存还是相当不错的,一定程度上缓解了数据库的IO操作,具体不多说,具体网上查找资料. 实战 不多说,直接上代码. 第一步:所需要的依赖 <!-- redis --> & ...