문제

I'd like to read multiline records that are terminated by null records, like so :

<MARQUE><AR_CP>N3</AR_CP>
<Classif>07</Classif>
<RegnT>03</RegnT>\0<MARQUE><AR_CP>O1</AR_CP>
<AR_PC>M5W 1C8</AR_PC>
<Classif>07</Classif>
<RegnT>03</RegnT>\0<MARQUE><AR_CP>Q1</AR_CP>
<Classif>07</Classif>
<RegnT>03</RegnT>
...

I would like to read the file, one record at a time. In perl I would use the "input record separator" $/ but I can't seem to find an equivalent in Groovy. Note that the file is too large to be slurped, and then simply split.

Is there an elegant way to do this in Groovy, or do I have to read byte per byte and "manually" break on '\0'?

Thank you!

도움이 되었습니까?

해결책

You could try using Scanner like this:

new File( '/path/to/file.txt' ).withReader { r ->
  new Scanner( r ).with { scanner ->
    scanner.useDelimiter( ~/\00/ )
    scanner.eachWithIndex { record, idx ->
      println "Record $idx ${record.split('\n').join()}"
    }
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top