Java 8+
import java.nio.charset.StandardCharsetsimport java.nio.file.{Files, Paths}val path = Paths.get("file.txt")new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
Java 11+
import java.nio.charset.StandardCharsetsimport java.nio.file.{Files, Path}val path = Path.of("file.txt")Files.readString(path, StandardCharsets.UTF_8)
These offer control over character encoding, and no resources to clean up. It's also faster than other patterns (e.g. getLines().mkString("\n")
) due to more efficient allocation patterns.