简体   繁体   中英

Reading a line with multiple line breaks as separate lines

I have java program which is running on mainframe z/os and is reading a EBCDIC file. Few of the lines in the file have multiple records separated by EBCDIC 'LF' X'025'. I am using Java readline and as expected it is reading the record till linefeed and discarding rest of the records. Besides parsing the big line into multiple records, is there any way to make the read methods split the line into multiple lines/records and return the same? If needed, I do have the option to change the new-line delimiter to any values.

Input:

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

Expected output

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

Current Code:

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;
           }
        } 

You're opening the file for QSAM binary I/O when you should be opening it as a text file stream.

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

Then just read the records as you normally would with a stream. There's no need for an additional line reader. z/OS UNIX newlines are usually x'15' so you may need to change your linefeed character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM