bsky
bsky

Reputation: 20242

Not all content of an array is written to a file

In Scala, I have large array of type Array[String] called lines which I am trying to print to a file like this:

val pw = new PrintWriter(new File("gene_test.counts"))
pw.write(lines.mkString("\n"))

However, not all the content is written to the file. I have debugged the program and it looks like the last 100 or so lines are not printed.

Why is this happening and how can I make the entire array be printed?

Upvotes: 0

Views: 41

Answers (1)

Andreas Neumann
Andreas Neumann

Reputation: 10904

To make sure everything is written you could flush the writer

val pw = new PrintWriter(new File("gene_test.counts"))
pw.write(lines.mkString("\n"))
pw.flush()

Another thing to keep in mind is to close the file and the writer.

A shorter solution could be

import scala.tools.nsc.io.File
File("fgene_test.counts").writeAll(lines mkString "\n")

Upvotes: 2

Related Questions