从txt文件中读取3行

时间:2014-02-25 14:20:53

标签: java io readline

txt文件读取和1~3行发送Mesaage和3~6行读取消息发送 但发送后读取1~3次,发送后再次读取1~3行... 我想要3行读取重复txt文件

的test.txt

1|11221234|c1|c2|c3|c4
2|11221234|c1|c2|c3|c4
3|11221234|c1|c2|c3|c4
4|11221234|c1|c2|c3|c4
5|11221234|c1|c2|c3|c4
6|11221234|c1|c2|c3|c4
7|11221234|c1|c2|c3|c4
8|11221234|c1|c2|c3|c4
9|11221234|c1|c2|c3|c4
9|11221234|c1|c2|c3|c4
10|11221234|c1|c2|c3|c4
11|11221234|c1|c2|c3|c4
12|11221234|c1|c2|c3|c4
13|11221234|c1|c2|c3|c4
                 while ((s = in.readLine()) != null) {
                    cnt++;
                    Element record = new Element("RECORD");
                    String[] arr = s.split("│");

                    if (arr.length == tableInfoMap.size()) {
                        for (int i = 0; i < arr.length; i++) {
                            int j = i + 1;
                            String fieldName = (String) tableInfoMap
                                    .get("COLUMN" + j);
                            Element field = new Element(fieldName);
                            field.addContent(new CDATA(arr[i]));
                            record.addContent(field);
                        }
                    } else {
                        throw new ArrayIndexOutOfBoundsException(" ");
                    }
                    rootElement.addContent(record);

                    if (cnt >= 3) {
                        sendMsg(srcTblName, tgtTblName, classPath,
                                className, doc, comm, sndAgency, rcvAgency,
                                cnt);
                        cnt = 0;
                    }
                }
                if (cnt > 0) {
                    boolean isSend = sendMsg(srcTblName, tgtTblName,
                            classPath, className, doc, comm, sndAgency,
                            rcvAgency, cnt);
                    in.close();
                    if (isSend == true) {

                        File f = new File(file);
                        if (f.delete() == true) {
                            logger.info("Send File Success: " + file);
                            logger.debug(f.getPath());
                        } else {
                            logger.warn("Send File Fail: " + file);
                            logger.debug(f.getPath());
                        }
                    }
                }

1 个答案:

答案 0 :(得分:1)

不确定问题是什么,但split()中的|字符需要转义(split()的参数是正则表达式而不是普通字符串):

String[] arr = s.split("\\│"); // Match | character only.