JAVA解析TXT格式文本文档

public void readRechargeWithdrawTxt(File file, Long noticeId) {
        List<CzCheckAccountInfoPO> records = new ArrayList<CzCheckAccountInfoPO>();// 
        // 将txt格式的数据存入数组
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(
                        new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = br.readLine();// 读取文件的方法
                String[] firstLine = lineTxt.split("\\|"); // 读第一行
                String withdrawCount = firstLine[0];
                String withdrawAmount = firstLine[1];
                String rechargeCount = firstLine[2];
                String rechargeAmount = firstLine[3];
                while ((lineTxt = br.readLine()) != null) {
                    String[] arrStrings = lineTxt.split("\\|"); // 用于把一个字符串分割成字符串数组
                    CzCheckAccountInfoPO record = new CzCheckAccountInfoPO();

                    record.setSerialNum(String.valueOf(noticeId));
                    record.setWithdrawCount(Integer.valueOf(withdrawCount));
                    record.setWithdrawAmount(new BigDecimal(withdrawAmount));
                    record.setRechargeCount(Integer.valueOf(rechargeCount));
                    record.setRechargeAmount(new BigDecimal(rechargeAmount));

                    String ymd = arrStrings[0];
                    String hms = arrStrings[1];
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    Date date = simpleDateFormat.parse(ymd + hms);
                    record.setTradeTime(date);
                    record.setMarketSerialNum(arrStrings[2]);
                    record.setSystemSerialNum(arrStrings[3]);
                    record.setBankCapitalNum(arrStrings[4]);
                    record.setTradeType(Integer.valueOf(arrStrings[5]));
                    record.setOriginator(Integer.valueOf(arrStrings[6]));
                    record.setAmount(new BigDecimal(arrStrings[7]));
                    record.setBalance(new BigDecimal(arrStrings[8]));
                    record.beforeInsert();
                    records.add(record); // 用set方法将取值分别添加到对应字符串数组 ,用add方法存入list

                }
                br.close();

            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }

        if(records!=null &&records.size()>0){
            czCheckAccountInfoManage.batchAdd(records);
        }
    }

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值