有些文件中存在Unicode字符和非Unicode字符,如何利用java快速的把文件中的Unicode字符转换为汉字而不影响文件中的其他字符呢,

我们知道虽然java 在控制台会把Unicode字符直接输出成汉字,但是当遇到文件中的Unicode和非Unicode字符在一起的时候却不好用了。

下面是代码,只需要把代码中的路径替换为你想要的路径,在建立一个转换后的文件路径。其他代码无需改变。

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader; public class Zhtest { public static void main(String[] args) throws IOException {
//源文件路径
String path = "d:\\Blaze.txt";
//输出文件路径
File write = new File("d:\\Blaze1.txt"); File file = null;
BufferedReader br = null;
BufferedWriter bw = new BufferedWriter(new FileWriter(write));
file = new File(path);
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
StringBuilder sb = new StringBuilder();
String length = "";
while ((length = br.readLine()) != null) {
sb.append(length);
bw.write(ascii2Native(sb.toString()) + "\r\n");
bw.flush();
sb = new StringBuilder();
} } public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf("\\u");
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf("\\u", begin);
}
sb.append(str.substring(begin));
return sb.toString();
} private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException("长度不足6位");
}
if (!"\\u".equals(str.substring(0, 2))) {
throw new IllegalArgumentException("字符必须以 \"\\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;
} }

如何利用java把文件中的Unicode字符转换为汉字的更多相关文章

  1. 类A是公共的,应在名为A.java的文件中声明错误

    第一种!!! “类A是公共的,应在名为A.java的文件中声明”这句话需要分两步来理解: 1.如果类A被声明为公共的(public),那么必须将类A保存在名为A.java的文件中: 2.反之,在一个文 ...

  2. macOS下利用dSYM文件将crash文件中的内存地址转换为可读符号

    一.使用流程 Windows下的程序运行崩溃时,往往可以利用pdb文件快速解析出程序崩溃的具体位置,甚至可以对应到源代码的具体行数.macOS下的symbolicatecrash也具备相应的功能.对应 ...

  3. Java入门-类HelloWorld是公共的,应在名为HelloWorld.java的文件中声明

    开始学习java了,搭好环境,notepad++中新建一个java文件,新建一个HelloWorld类, public class HelloWorld { public static void ma ...

  4. 报错:①Tog goal specified requires a project to execute but there is no POM in this directory......②说类HelloWorld是公共的, 应在名为 HelloWorld.java 的文件中声明 public class HelloWorld......

    在运行Maven的命令时,在DOS窗口里面必须把目录切换到项目的根部,要不然命令是找不到目的地. 下图是错误示范,项目在Demo02这个目录里,就必须将目录切换到Demo02下,否则DOS窗口只有飘红 ...

  5. 从视频文件中读入数据-->将数据转换为灰度图-->对图像做canny边缘检测-->将这三个结构显示在一个图像中

    //从视频文件中读入数据-->将数据转换为灰度图-->对图像做canny边缘检测-->将这三个结构显示在一个图像中 //作者:sandy //时间:2015-10-10 #inclu ...

  6. 三种java 去掉字符串中的重复字符函数

    三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...

  7. Wpf中显示Unicode字符

    1. 引言 今天在写一个小工具,里面有些字符用Unicode字符表示更合适.但是一时之间却不知道怎么写了.经过一番查找,终于找到了办法.记到这里,一是加深印象,二则以备查询. 2. C#中使用Unic ...

  8. go从文件中读取json字符串并转换

    go从文件中读取json字符串并转换 将要读取的文件的一部分 [ { "children": [ { "children": [ { "code&qu ...

  9. 删除文件中的 ^M 字符

    删除文件中的 ^M 字符 有时候,我们在 Linux 中打开曾在 Win 中编辑过的文件时,会在行尾看到 ^M 字符.虽然,这并不影响什么,但心里面还是有点不痛快.如果想要删除这些 ^M 字符,可以使 ...

随机推荐

  1. 别人写的一个Bootstrap系列教程

    http://www.cnblogs.com/lansy/category/659061.html

  2. Sql清理日志文件

    场景: 我们导入MR数据时发现磁盘空间不够用了,导致的结果就是我们的程序很可能会抛出异常了,我们需要导入数据的时候进行日志瘦身. 问1:导入数据的时候,瘦身是否会造成数据库的异常? DBA提供解决方案 ...

  3. 在xocde运行profile 遇到"Existing default base temp directory '/Library/Caches/com.apple.dt.instruments' has insufficient privileges for user id 505. Please have the owner delete this directory"

    找到这篇文章 http://stackoverflow.com/questions/34153795/xcode-7-unable-to-open-instruments-from-developer ...

  4. C++Primer 第十四章

    //1.当运算符作用于类类型运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令程序更加易于编写和阅读. //2.重载的运算符是具有特殊名字的函数,它们由关键字operato ...

  5. How to debug PostgreSQL function with pgAdminIII

    How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...

  6. Spring中Bean的配置:基于注解的方式

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: -@Component:基本注解,标识一个受Spring管理的组件 -@Respositor ...

  7. struts文件上传(单文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. struts配置通配符*来匹配方法,实现动态调用

    01:web.xml中配置,启动struts2 <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  9. Android Notification通知栏使用

    package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...

  10. 关于DISTINCE的用法

    SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法 ...