Home > Enterprise >  In what mode are files opened in Scala Files IO?
In what mode are files opened in Scala Files IO?

Time:09-17

Python file handling function takes mode as optional parameter which for default is "r" (read).

open(file, mode) #where mode by default is "r"

What is the Scala's analogous default for the same ?

CodePudding user response:

In case of using the concise one-liner method scala.io.Source.fromFile, file is opened in read mode, even if the same method is used to get reference to the buffered source. Besides, to write TXT files we use Java fallbacks of PrintWriter or FileWriter, which opens files in write mode. Hence the "default" depends on whether we are writing to the file or reading from it.

I figured this by filtering the open files with lsof after opening file in sbt console, any productive additions and/or corrections are appreciated.

  • Related