工具使用:
package test.opservice;

import eh.util.MapValueUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; /**
* Created by houxr on 2016/11/14.
*/
public class ImportParseText { public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + MapValueUtil.ascii2native(tempString));
line++;
}
System.out.println("读取结果:" + tempString);
System.out.println("转换后结果:" + MapValueUtil.ascii2native(tempString));
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
} public static void main(String[] args) {
readFileByLines("D:/dekeResult.txt");
}
}

工具类:

package eh.util;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils; import java.util.Date;
import java.util.Map; /**
* author:houxr
* date:2016/6/2.
*/
public class MapValueUtil { public static String getString(Map<String, Object> map, String key) {
Object obj = getObject(map, key);
if (null == obj) {
return "";
} if (obj instanceof String) {
return obj.toString();
} return "";
} public static Integer getInteger(Map<String, Object> map, String key) {
Object obj = getObject(map, key);
if (null == obj) {
return null;
} if (obj instanceof Integer) {
return (Integer) obj;
} if (obj instanceof String) {
return Integer.valueOf(obj.toString());
} return null;
} public static Date getDate(Map<String, Object> map, String key) {
Object obj = getObject(map, key);
if (null == obj) {
return null;
} if (obj instanceof Date) {
return (Date) obj;
} return null;
} public static Float getFloat(Map<String, Object> map, String key) {
Object obj = getObject(map, key);
if (null == obj) {
return null;
} if (obj instanceof Float) {
return (Float) obj;
} return null;
} public static Double getDouble(Map<String, Object> map, String key) {
Object obj = getObject(map, key);
if (null == obj) {
return null;
} if (obj instanceof Double) {
return (Double) obj;
} if (obj instanceof Float) {
return Double.parseDouble(obj.toString());
} if (obj instanceof Integer) {
return Double.parseDouble(obj.toString());
} return null;
} public static Object getObject(Map<String, Object> map, String key) {
if (null == map || StringUtils.isEmpty(key)) {
return null;
} return map.get(key);
} /**
* json 转换为 实体对象
*
* @param str
* @param type
* @param <T>
* @return
*/
public static <T> T fromJson(String str, Class<T> type) {
Gson gson = new Gson();
try {
T t = gson.fromJson(str, type);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* asciicode 转为中文
*
* @param asciicode eg:{"code":400002,"msg":"\u7b7e\u540d\u9519\u8bef"}
* @return eg:{"code":400002,"msg":"签名错误"}
*/
public static String ascii2native(String asciicode) {
String[] asciis = asciicode.split("\\\\u");
String nativeValue = asciis[0];
try {
for (int i = 1; i < asciis.length; i++) {
String code = asciis[i];
nativeValue += (char) Integer.parseInt(code.substring(0, 4), 16);
if (code.length() > 4) {
nativeValue += code.substring(4, code.length());
}
}
} catch (NumberFormatException e) {
return asciicode;
}
return nativeValue;
} }

Java读取本地文件进行unicode解码的更多相关文章

  1. Java读取本地文件,并显示在JSP文件中

        当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...

  2. Java读取本地文件(输入流)

    package cn.buaa; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; imp ...

  3. java 读取本地文件并转换为byte数组

    private byte[] InputStream2ByteArray(String filePath) throws IOException { InputStream in = new File ...

  4. java读取本地文件

    File file = new File("F:/hejing/InstrumentJsonData.txt");        StringBuilder localStrBul ...

  5. java 中读取本地文件中字符

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  6. JAVA读取XML文件并解析获取元素、属性值、子元素信息

    JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取  InputStream   DocumentBuilderFactory   Element     Node 前言 最 ...

  7. .NET 读取本地文件绑定到GridViewRow

    wjgl.aspx.cs: using System; using System.Collections; using System.Configuration; using System.Data; ...

  8. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  9. 前台JS(type=‘file’)读取本地文件的内容,兼容各种浏览器

    [自己测了下,能兼容各种浏览器,但是读取中文会出现乱码.自己的解决方法是用notepad++把txt文件编码改为utf-8(应该是和浏览器编码保持一致吧?..)] 原文  http://blog.cs ...

随机推荐

  1. 《算法导论》习题解答 Chapter 22.1-3(转置图)

    一.邻接表实现 思路:一边遍历,一边倒置边,并添加到新的图中 邻接表实现伪代码: for each u 属于 Vertex for v 属于 Adj[u] Adj1[v].insert(u); 复杂度 ...

  2. Spring学习总结一——SpringIOC容器一

    什么是spring spring是一个开源的轻量级的应用开发框架,它提供了IOC和AOP应用,可以减少组件之间的耦合度,即 解耦,spring容器可以创建对象并且管理对象之间的关系. 一:实例化spr ...

  3. 为DEDE织梦添加XMl网站地图

    在后台管理: 核心-频道模型-单页文档管理-增加一个新页面 模版文件放在你现在使用的templets目录下,sitemap.xml的内容如下 <?xml version="1.0&qu ...

  4. Python(2.7.6) 标准日志模块 - Logging Configuration

    除了使用 logging 模块中的 basicConfig 方法配置日志, Python 的 logging.config 模块中, dictConfig 和 fileConfig 方法分别支持通过字 ...

  5. 【Quote】What is Mono

    What is Mono Mono is a software platform designed to allow developers to easily create cross platfor ...

  6. Mac下kernel_task占用大量CPU怎么办?

    我们都知道要想让电脑运行的快速,那么就要尽量的保持内存和CPU的充足.不过一些MAC用户发现MAC系统中的Kernel_task会占用大量的CPU,导致电脑发热变卡.这个问题该怎么解决呢? 具体操作步 ...

  7. Android 混淆与混淆过滤

    Android 中代码混淆一般用的是ProGuard.它除了混淆代码之后还有其它许多实用的功能.这里主要记录混淆相关的实现. 1.ProGuard的作用 删除无用代码,压缩和优化Class文件,缩小A ...

  8. JavaScript之动画3

    给一个div添加颜色,使其产生渐变效果,我们设置index为变量,使用setInterval函数方法改变rgb颜色值. window.onload = function(){ var boxDom = ...

  9. SQL IDENTITY(int,1,1) 用法

    select IDENTITY(int,1,1) as SortID from tb_order 仅当 SELECT 语句中有 INTO 子句时,才能使用 IDENTITY 函数. select ID ...

  10. ASP.NET 使用 System.Web.Script.Serialization 解析 JSON (转)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...