Writing to a file with Groovy (Grails) does not work for some lines (broken lines)

I am bulk recording to a CSV file using Groovy. More specifically, I have a Quartz job that does, and creates some Map messages that are sent to the RabbitMQ queue. The queue is consumed by 10 consumers and leads to the creation of some line lists. For each item in a List, I simply write it in a file divided by the .csv channel. The actual service, which has a method that writes to a CSV file, is a standard (Singleton) grails transactional service. When I write the lines to be written, everything is fine, but some lines in the file are “broken”. I write:

def writeRowsToFile(List<String> rows, File file) {
  rows.each {row->
    file.append("${row}\n")
  }
}

I originally used:

file.withWriterAppend {out->
  out.write(row.toString())
  out.newLine()
}

and got the same thing as ...

- , . - , concurrency , ?

.

+3
1

, :

def writeRowsToFile(List<String> rows, File file) {
  file.withWriterAppend {out->
    rows.eachWithIndex { row, idx ->

      // It probably \n chars in your strings
      if( row ==~ /.*[\n\r]+.*/ ) {
        println "Detected a CRLF char in rows[$idx]"
      }

      out.writeLine row
    }
  }
}

, "- "

?

, , row \n

+3

Source: https://habr.com/ru/post/1794411/


All Articles