工具使用:
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. 将Winform程序快速转换为在浏览器中运行的程序

    http://www.codeproject.com/Articles/31429/Embedding-a-NET-WinForms-Application-in-an-Interne 详见以上文章. ...

  2. SpringMVC 的 Controller 返回各种视图的处理方式

    SpringMVC 的 Controller 可以返回各种各样的视图.比如 JSP, JSON, Velocity, FreeMarker, XML, PDF, Excel, 还有Html字符流 等等 ...

  3. HDU 2571 命运 (DP)

    命运 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  4. hdu-5714 拍照(二分)

    题目链接: 拍照 Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Proble ...

  5. 转: 58同城高性能移动Push推送平台架构演进之路

    转: http://geek.csdn.net/news/detail/58738 文/孙玄 本文详细讲述58同城高性能移动Push推送平台架构演进的三个阶段,并介绍了什么是移动Push推送,为什么需 ...

  6. Unity3d之动态连接Mesh Renderer和Collider

    using UnityEngine; using System.Collections; public class dynaMesh : MonoBehaviour {     public Skin ...

  7. DWZ (JUI) 教程 DWZ中dialog层的刷新

    在DWZ开发过程中经常会遇到的一种情况就是:在navTab页面中通过a标签打开一个dialog,在dialog层进行操作后,需要对该dialog层进行必要的刷新操作. 1.首先讲一下思路: 在非dia ...

  8. .NET使用NPOI读取Word模板并替换关键字并下载

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  9. 集合类学习之ArrayList源码解析

    1.概述 ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大 ...

  10. UI2_UISwitch与UIActivity

    // // ViewController.m // UI2_UISwitch与UIActivity // // Created by zhangxueming on 15/7/7. // Copyri ...