pythonic
pythonic

Reputation: 21615

What is the fastest way to read an entire file into a String in Scala?

I am currently using the following to do this.

val string = scala.io.Source.fromFile(filePath).mkString

However, I've noticed that it is quite slow. Is there any better (in terms of speed) method to read the whole file into a string?

Upvotes: 1

Views: 3286

Answers (1)

pythonic
pythonic

Reputation: 21615

I used the following. This is much faster than my previous approach.

import java.nio.file.Files
import java.nio.file.Paths

val string = new String(Files.readAllBytes(Paths.get(filePath)))

Upvotes: 2

Related Questions