Quantcast
Viewing latest article 12
Browse Latest Browse All 20

Answer by Muyyatin for Read entire file in Scala?

Using getLines() on scala.io.Source discards what characters were used for line terminators (\n, \r, \r\n, etc.)

The following should preserve it character-for-character, and doesn't do excessive string concatenation (performance problems):

def fileToString(file: File, encoding: String) = {  val inStream = new FileInputStream(file)  val outStream = new ByteArrayOutputStream  try {    var reading = true    while ( reading ) {      inStream.read() match {        case -1 => reading = false        case c => outStream.write(c)      }    }    outStream.flush()  }  finally {    inStream.close()  }  new String(outStream.toByteArray(), encoding)}

Viewing latest article 12
Browse Latest Browse All 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>