工具使用:
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. C#中Dictionary小记

    使用C#中Dictionary的一下细节小记: 一.Dictionary.Add(key,value) 与 Dictionary[key]=value的区别: 如果Dictionary中已经有了key ...

  2. ReactNative学习-webView

    在软件内部打开一个网页--不喜欢它没有办法返回,还需要再添加返回按钮== import React from 'react'; import { AppRegistry, Component, Scr ...

  3. Input框去掉蓝色边框

    Input框去掉蓝色边框: <input type="text" name="" value="" class="Inpt& ...

  4. POJ 3660 Cow Contest (闭包传递)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7690   Accepted: 4288 Descr ...

  5. php学习笔记4--php中的变量作用域

    变量作用域:可以简单地理解为变量的可见区域,变量能被访问的范围.如同其他语言,php中也有全局作用域和局部作用域之分,但是不同的是:php中的全局作用域指的是:只能在函数外部使用,而局部作用域指的是: ...

  6. Java之阶乘数的计算

    说起“阶乘数”,我们应该都不会感到陌生.当老师布置了这样的作业,我们大多数人是一贯用笔算,还有的同学会用计算机去计算.数学是讲究原理和方法的,我们知其然,也要知其所以然.下面我们就用编程来计算阶乘数. ...

  7. php读取目录下的文件

    工作需要写了一个读取指定目录下的文件,并显示列表,点击之后读取文件中的内容 高手拍砖,目录可以自由指定,我这里直接写的是获取当前文件目录下面的所有文件 <?php /** * 读取指定目录下面的 ...

  8. 调用支付宝PHP接口API实现在线即时支付功能(UTF-8编码)

    这次在项目中要实现订单功能,所以要完成在线支付,在线支付一般有网银支付和第三方支付(支付宝.paypal等)这两种途径,未简单起见,先完成支付宝在线支付功能,由于项目基于Yii框架,且使用UTF-8编 ...

  9. Cocos2d-js中使用纹理对象创建Sprite对象

    本节我们会通过一个实例介绍纹理对象创建Sprite对象使用,这个实例如图5-2所示,其中地面上的草是放在背景(如下图所示)中的,场景中的两棵树是从后图所示的“树”纹理图片中截取出来的,图5-5所示是树 ...

  10. cocos2d-x 创建工程详解

    我们的编写的第一个程序一般习惯上都命名为HelloWorld,从它开始再学习其他的内容.下面介绍的第一个Cocos2d-x游戏我们也命名为HelloWorld. 创建工程 在Cocos2d-x早期版本 ...