Quantcast
Channel: Read entire file in Scala? - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by Paul Draper for Read entire file in Scala?

$
0
0

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.


Viewing all articles
Browse latest Browse all 20

Trending Articles



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