Writing to file with Groovy(Grails) fails for some lines (broken lines)

谁都会走 提交于 2019-12-07 17:55:32

问题


I am performing some mass writing in a .csv file using Groovy. More specifically, I have a Quartz job that is running and creates some Map messages that get sent to a RabbitMQ queue. The queue is being consumed by 10 consumers and results in producing some lists of Strings. For each element in the List I just write it in a pipe separated .csv file. The actual service that has the method that writes to the .csv file, is a standard (singleton) transactional grails service. When I log the lines to be written, everything's fine, but in the file, some lines are "broken". The way I am writing is:

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

Initially I was using:

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

and got the same thing as well...

If it was something wrong it would fail for all the lines. Could it be some kind of race condition, concurrency or I don't know what else issue?

Any help will be appreciated.

Thanks


回答1:


You should be doing it the second way, ie:

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

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

      out.writeLine row
    }
  }
}

However, you say it might be "some kind of race condition"

Are multiple threads writing to the same file?

If not, it is more likely that your row data has \n characters in it



来源:https://stackoverflow.com/questions/5104074/writing-to-file-with-groovygrails-fails-for-some-lines-broken-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!