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来编写,可 ...
随机推荐
- HDOJ5875(线段树)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- LogUtils日志管理工具
public class LogUtils { public static final int VERBOSE = 1; public static final int DEBUG = 2; publ ...
- appium+python自动化28-name定位
前言 appium1.5以下老的版本是可以通过name定位的,新版本从1.5以后都不支持name定位了 name定位报错 1.最新版appium V1.7用name定位,报错: selenium.co ...
- 配置PHP,Apache
安装完windows 2003 server以后,还是个裸机,在安装limesurvey总是会有些问题,还好,问题都解决了,下面讲下配置的步骤: 第一步:先装上apache服务 apache服务启动以 ...
- Go - 指针简介 与 ++/--运算符以及控制语句
指针 Go 语言中,对于指针有一些特殊约束: 1. 不在支持 “->” 符号,所有的指针使用“.” 来操作指针对象的成员变量 2. 指针的默认值为 “nil” ++ 与 -- 作为语句而非表达式 ...
- 【学习笔记】dp入门
知识点 动态规划(简称dp),可以说是各种程序设计中遇到的第一个坎吧,这篇博文是我对dp的一点点理解,希望可以帮助更多人dp入门. 先看看这段话 动态规划(dynamic programming) ...
- Oracle block 格式
Oracle block 格式 信息参考: http://www.ixora.com.au/ 特别感谢 overtime 大哥对我的无私的帮助和对我一直鼓励支持我的网友这些资料是没得到oracle ...
- python覆盖率统计
啦啦啦,最近在跟离线脚本写自动化,真麻烦呀~ 离线任务是python写的,自动化写完了,就得统计覆盖率了. coverage.py是一个用来统计python程序代码覆盖率的工具.网上有很多资料,使用起 ...
- 第六章 声明式服务调用: Spring Cloud Feign
我们在使用 Spring Cloud Ribbon 时, 通常都会利用它对 RestTemplate 的请求拦截来实现对依赖服务的接口调用, 而 RestTemplate 已经实现了对 HTTP 请求 ...
- tkinter实现的文本编辑器
效果: # -*- encoding: utf8 -*- #python 2.7 from Tk ...