Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将具有多个换行符的行作为单独的行读取_Java_Multiline - Fatal编程技术网

Java 将具有多个换行符的行作为单独的行读取

Java 将具有多个换行符的行作为单独的行读取,java,multiline,Java,Multiline,我有一个java程序,它运行在大型机z/os上,正在读取一个EBCDIC文件。文件中很少有行具有多条记录,这些记录由EBCDIC“LF”X“025”分隔。我正在使用Java readline,正如预期的那样,它正在读取记录,直到linefeed,并丢弃其余的记录。除了将大行解析为多个记录外,还有什么方法可以让read方法将行拆分为多行/记录并返回相同的结果?如果需要,我可以选择将新行分隔符更改为任何值 输入: 10,IBM,ERWIN,BLACK,123,ABC(LF)10,IBM,JACK,R

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

输入:

10,IBM,ERWIN,BLACK,123,ABC(LF)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;
           }
        } 
预期产出

10,IBM,ERWIN,BLACK,123,ABC
10,IBM,JACK,ROSE,456
当前代码:

10,IBM,ERWIN,BLACK,123,ABC(LF)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;
           }
        } 
public ArrayList readMainframeFile()
{
//字符串DDNAME=ZFile.allocDummyDDName();
//System.out.println(“DDName为”+DDName);
//ZFile-convolut=newzfile(//DD:convolut”,“wb,type=record,nosek”);
//RecordReader=null;
试一试{
ZFile-convin=newzfile(//DD:convin”,“rb,type=record,nosek”);
System.out.println(“DDName是”+convin.getLrecl());
byte[]recordBuf=新字节[convin.getLrecl()];
int字节读取=0;
InputStream ins=null;
BufferedReader reader=null;
字符串temp=null;
ArrayList行=新的ArrayList();
而((bytesRead=convin.read(recordBuf))>0){
//System.out.println(“读取的字节数为”+字节读取);
试一试{
ins=新的ByteArrayInputStream(recordBuf);
reader=新的BufferedReader(新的InputStreamReader(ins,Charset.forName(“ibm500”));
temp=reader.readLine();
行。添加(临时);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
} 
回流线;
}捕获(ZFileE异常){
System.err.println(e.getMessage());
返回null;
}
} 

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

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

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

拆分换行符的基本条件是什么?我猜它的
(LF)
应该循环
readLine()
,直到它返回null。在构造
ByteArrayInputStream时,您还忽略了
ByteRead