在日常开发中,有时候需要对第三方文件推送过来的数据文件进行解析,双方约定的格式是GBK编码,一行代表一条记录,字节定长的方式去存入和获取,这样就不能用substring进行字符截取了,具体实现方法直接看代码吧:
/** * 解析第三方传入文件 * * @param filePath * 传入文件路径 * @throws Exception */ public static void parseFile(String filePath) throws Exception { try { File file = new File(filePath); InputStream is = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(is,Charset.forName("GBK"))); String line = ""; while ((line = br.readLine()) != null) { // 客户名-20位-截取6-25 String cifName= StringCommonUtil.substringByte(line,6, 19).trim(); // 身份证号-18位-截取31-48 String blackListType = StringCommonUtil.substringByte(line,31, 17).trim(); //todo其他业务处理 }