Java读取本地文件进行unicode解码
工具使用:
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解码的更多相关文章
- Java读取本地文件,并显示在JSP文件中
当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...
- Java读取本地文件(输入流)
package cn.buaa; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; imp ...
- java 读取本地文件并转换为byte数组
private byte[] InputStream2ByteArray(String filePath) throws IOException { InputStream in = new File ...
- java读取本地文件
File file = new File("F:/hejing/InstrumentJsonData.txt"); StringBuilder localStrBul ...
- java 中读取本地文件中字符
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- JAVA读取XML文件并解析获取元素、属性值、子元素信息
JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取 InputStream DocumentBuilderFactory Element Node 前言 最 ...
- .NET 读取本地文件绑定到GridViewRow
wjgl.aspx.cs: using System; using System.Collections; using System.Configuration; using System.Data; ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 前台JS(type=‘file’)读取本地文件的内容,兼容各种浏览器
[自己测了下,能兼容各种浏览器,但是读取中文会出现乱码.自己的解决方法是用notepad++把txt文件编码改为utf-8(应该是和浏览器编码保持一致吧?..)] 原文 http://blog.cs ...
随机推荐
- 02. SQL表达式的灵活使用
什么是SQL表达式?在SQL语句中,表达式可以是函数,也可以是列和列之间的混合运算.很多时候,对于表达式的使用,可以比单独操作表上的列,带来更多方便. 一. 在HAVING中使用表达式 --drop ...
- 百篇大计敬本年之系统篇《六》—— Ubuntu 16.04开启 root 超级用户
Ubuntu 16.04系统在一开始安装完成时是无法切换到 root 用户的,普通用户需要使用 root 权限的时候通常需要在执行命令前加 "sudo",需要经常使用root权限的 ...
- 跨越跳板机传文件nc
从线上服务器与本机互传文件 传输方 nc -l 10000 < a.tar 接收方 nc xx.xx.xx.xx 10000 >a.tar 原理: 文件传输方运行nc,指定端口,设置监听文 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- 改变WPF ListBoxItem的选中样式
想用ListBox作一个类似IOS 设置的菜单,却发现很难改变ListBoxItem鼠标移过.选中的默认蓝色背景与边框. 尝试使用Style来设置strigger,依然不成功.在百度搜索一些资料,提到 ...
- 应用越来越广泛的css伪类
说起css伪类,学习web前端网页设计的同学们应该对此应该不是很陌生,以前很多的网页的特效大多是通过js来实现的.但是随着CSS3不断开发,利用css实现网页的特效不仅响应不错,而且还减少了很多的代码 ...
- Bootstrap 小技巧以及相关资源整理
1, Bootstrap Bundle (http://bootstrapbundle.com/): 提供了15中不同的MVC Bootstrap模板.[扩展和更新]中搜索“Bootstrap Bu ...
- 北大ACM(POJ1003-Hangover)
Question:http://poj.org/problem?id=1003问题点:水题. Memory: 220K Time: 0MS Language: C++ Result: Accepted ...
- [老老实实学WCF] 第四篇 初探通信--ChannelFactory
老老实实学WCF 第四篇 初探通信--ChannelFactory 通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了. ...
- python简单爬虫编写
1.主要学习这程序的编写思路 a.读取解释网站 b.找到相关页 c.找到图片链接的元素 d.保存图片到文件夹 ..... 将每一个步骤都分解出来,然后用函数去实现,代码易读性高. ##代码尽快运行时会 ...