JAVA解析TXT格式文本文档

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

                    ((noticeId));
                    ((withdrawCount));
                    (new BigDecimal(withdrawAmount));
                    ((rechargeCount));
                    (new BigDecimal(rechargeAmount));

                    String ymd = arrStrings[0];
                    String hms = arrStrings[1];
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    Date date = (ymd + hms);
                    (date);
                    (arrStrings[2]);
                    (arrStrings[3]);
                    (arrStrings[4]);
                    ((arrStrings[5]));
                    ((arrStrings[6]));
                    (new BigDecimal(arrStrings[7]));
                    (new BigDecimal(arrStrings[8]));
                    ();
                    (record); // 用set方法将取值分别添加到对应字符串数组 ,用add方法存入list

                }
                ();

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

        if(records!=null &&()>0){
            (records);
        }
    }