工具使用:
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. php实现签到功能

    首先我在数据库里建了两张表,一个是用户的积分表,一个是签到状态表,分来用来记录用户的积分数和先到状态 在用户签到状态表中我们有一个字段,last_sign_time,即上一次签到时间,每次可以签到的时 ...

  2. Linux Centos 7 使用yum安装 mysql5.7 (实验成功)

    第一部分:安装Mysql5.7 1.下载YUM库 shell > wget http://dev.mysql.com/get/mysql57-community-release-el7-7.no ...

  3. Spring学习总结三——SpringIOC容器三

    一:spring容器自动装配注入 为了减少xml中配置内容,可以使用自动装配注入,代替setter注入,只需要在 bean对象配置中添加属性autoWire即可,那么在类中就会自动扫描setXXX() ...

  4. ASP.NET 状态的传递和保存

    1,HTTP协议是无状态的.服务器不会记住上次给浏览器的处理结果,如果需要上次处理结果(上次状态)就需要浏览器把处理结果值(上次状态)再次给服务器. 2,URL传值:通过URL参数或者通过Form表单 ...

  5. 【CSS3】---结构性伪类选择器—nth-child(n)+nth-last-child(n)

    结构性伪类选择器—nth-child(n) “:nth-child(n)”选择器用来定位某个父元素的一个或多个特定的子元素.其中“n”是其参数,而且可以是整数值(1,2,3,4),也可以是表达式(2n ...

  6. Server.MapPath()获取绝对路径

    1.    Server.MapPath("/")  应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Server.MapPath("./&qu ...

  7. JS 回车提交

    1.JavaScript 方法: <script> document.onkeydown=function(event){ e = event ? event :(window.event ...

  8. centos7没有安装ifconfig命令的解决方法

    ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息,可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令,这时候你进入终端,运行ifc ...

  9. 通信行业OSS支撑系统软件研发思考

    一般的,对所谓大型.通信行业.OSS支撑软件系统,我们可宏观定义以下几点: 以年计的研发周期 以几十人计的研发团队 以百计的业务菜单功能点 以千计的数据库表 以万计的业务术语指标 以亿计的数据表记录 ...

  10. Windows下用cmd命令安装及卸载服务

    第一种方法: 1. 开始 ->运行 ->cmd2. cd到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(Framework版本号按IIS配置) ...