工具使用:
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":"u7b7eu540du9519u8bef"} * @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; } }