Pregunta

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!

¿Fue útil?

Solución

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()}"
    }
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top