【问题标题】:Reading a line with multiple line breaks as separate lines读取具有多个换行符的行作为单独的行
【发布时间】:2015-03-20 22:32:51
【问题描述】:

我有一个在大型机 z/os 上运行的 java 程序,它正在读取一个 EBCDIC 文件。文件中的几行有多个由 EBCDIC 'LF' X'025' 分隔的记录。我正在使用 Java readline 并且正如预期的那样,它正在读取记录直到换行并丢弃其余记录。除了将大行解析为多条记录外,还有什么方法可以使读取方法将行拆分为多条/记录并返回相同的内容?如果需要,我可以选择将换行符更改为任何值。

输入:

10,IBM,ERWIN,BLACK,123,ABC(LF)10,IBM,JACK,ROSE,456

预期输出

10,IBM,ERWIN,BLACK,123,ABC
10,IBM,JACK,ROSE,456

当前代码:

public ArrayList<String> readMainframeFile()
    {
        //String DDNAME = ZFile.allocDummyDDName();
        //System.out.println("The DDName is " + DDNAME);
        //ZFile convout = new ZFile("//DD:CONVOUT", "wb,type=record,noseek");

        //RecordReader reader=null;

        try {
            ZFile convin = new ZFile("//DD:CONVIN", "rb,type=record,noseek");
            System.out.println("The DDName is" + convin.getLrecl());
            byte[] recordBuf = new byte[convin.getLrecl()];
            int bytesRead=0;
            InputStream ins = null;
            BufferedReader reader = null;
            String temp=null;
            ArrayList<String> lines = new ArrayList<String>();

            while((bytesRead = convin.read(recordBuf)) > 0) {
                //System.out.println("The number of bytes read is" + bytesRead);
                try {
                    ins = new ByteArrayInputStream(recordBuf);
                    reader = new BufferedReader(new InputStreamReader(ins,Charset.forName("ibm500")));
                    temp=reader.readLine();
                    lines.add(temp);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }

            } 
            return lines;

        } catch (ZFileException e) {
            System.err.println(e.getMessage());
            return null;
           }
        } 

【问题讨论】:

  • 分割线的基本标准是什么?我猜是(LF)
  • 你应该循环readLine()直到它返回null。您在构建 ByteArrayInputStream. 时也忽略了 bytesRead

标签: java multiline


【解决方案1】:

当您应该将文件作为文本文件流打开时,您正在为 QSAM 二进制 I/O 打开文件。

ZFile convin = new ZFile("//DD:CONVIN", "r");

然后像平常使用流一样读取记录。无需额外的线路阅读器。 z/OS UNIX 换行符通常是 x'15',因此您可能需要更改换行符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2020-05-07
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多