Quantcast
Channel: Read entire file in Scala? - Stack Overflow
Browsing latest articles
Browse All 20 View Live
↧

Answer by Y2Kot for Read entire file in Scala?

You can useSource.fromFile(fileName).getLines().mkStringhowever it should be noticed that getLines() removes all new line characters.If you want save formatting you should...

View Article


Answer by Li Haoyi for Read entire file in Scala?

If you don't mind a third-party dependency, you should consider using my OS-Lib library. This makes reading/writing files and working with the filesystem very convenient:// Make sure working directory...

View Article


Answer by Apurw for Read entire file in Scala?

import scala.io.sourceobject ReadLine{def main(args:Array[String]){if (args.length>0){for (line <- Source.fromLine(args(0)).getLine())println(line)}}in arguments you can give file path and it...

View Article

Answer by Paul Draper for Read entire file in Scala?

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...

View Article

Answer by comonad for Read entire file in Scala?

You do not need to parse every single line and then concatenate them again...Source.fromFile(path)(Codec.UTF8).mkStringI prefer to use this:import scala.io.{BufferedSource, Codec, Source}import...

View Article


Answer by pathikrit for Read entire file in Scala?

One more: https://github.com/pathikrit/better-files#streams-and-codecsVarious ways to slurp a file without loading the contents into memory:val bytes : Iterator[Byte] = file.bytesval chars :...

View Article

Answer by elm for Read entire file in Scala?

For emulating Ruby syntax (and convey the semantics) of opening and reading a file, consider this implicit class (Scala 2.10 and upper),import java.io.Filedef open(filename: String) = new...

View Article

Answer by Dzmitry Lazerka for Read entire file in Scala?

Just like in Java, using CommonsIO library:FileUtils.readFileToString(file, StandardCharsets.UTF_8)Also, many answers here forget Charset. It's better to always provide it explicitly, or it will hit...

View Article


Answer by elm for Read entire file in Scala?

For faster overall reading / uploading a (large) file, consider increasing the size of bufferSize (Source.DefaultBufSize set to 2048), for instance as follows,val file = new...

View Article


Answer by Atiq for Read entire file in Scala?

you can also use Path from scala io to read and process files.import scalax.file.PathNow you can get file path using this:- val filePath = Path("path_of_file_to_b_read", '/')val lines =...

View Article

Answer by gordonpro for Read entire file in Scala?

print every line, like use Java BufferedReader read ervery line, and print it:scala.io.Source.fromFile("test.txt" ).foreach{ print }equivalent:scala.io.Source.fromFile("test.txt" ).foreach( x =>...

View Article

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...

View Article

Answer by poko for Read entire file in Scala?

as a few people mentioned scala.io.Source is best to be avoided due to connection leaks. Probably scalax and pure java libs like commons-io are the best options until the new incubator project (ie...

View Article


Answer by psp for Read entire file in Scala?

(EDIT: This does not work in scala 2.9 and maybe not 2.8 either)Use trunk:scala> io.File("/etc/passwd").slurpres0: String = ### User Database# ... etc

View Article

Answer by Ikai Lan for Read entire file in Scala?

I've been told that Source.fromFile is problematic. Personally, I have had problems opening large files with Source.fromFile and have had to resort to Java InputStreams.Another interesting solution is...

View Article


Answer by Daniel Spiewak for Read entire file in Scala?

Just to expand on Daniel's solution, you can shorten things up tremendously by inserting the following import into any file which requires file manipulation:import scala.io.Source._With this, you can...

View Article

Answer by oxbow_lakes for Read entire file in Scala?

The obvious question being "why do you want to read in the entire file?" This is obviously not a scalable solution if your files get very large. The scala.io.Source gives you back an Iterator[String]...

View Article


Answer by Walter Chang for Read entire file in Scala?

// for file with utf-8 encodingval lines = scala.io.Source.fromFile("file.txt", "utf-8").getLines.mkString

View Article

Answer by Daniel C. Sobral for Read entire file in Scala?

val lines = scala.io.Source.fromFile("file.txt").mkStringBy the way, "scala." isn't really necessary, as it's always in scope anyway, and you can, of course, import io's contents, fully or partially,...

View Article

Read entire file in Scala?

What's a simple and canonical way to read an entire file into memory in Scala? (Ideally, with control over character encoding.)The best I can come up with...

View Article
Browsing latest articles
Browse All 20 View Live


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