Scala File handling
Scala provides predefined methods to deal with file. You can create, open, write and read file. Scala provides a complete package scala.io for file handling.
In this chapter, we will discuss all these file operations in detail.
Scala Creating a File Example
Scala doesn't provide file writing methods. So, you have to use the Java PrintWriter or FileWriter methods.
The above code will create a text file ScalaFile.txt. After creating file printwriter is used to write content to this file.
Scala Reading File Example: Reading Each Charecter
H e l l o , T h i s i s s c a l a f i l e
Scala provides file reading methods. In the above code, we have imported scala.io package and Source class is used to access file related methods like fromFile().
You must close file refernce after reading or writing file by using close() method.
Scala Reading a File Example: Reading Each Line
Hello, This is scala file
Apart from these for all kind of file handling operations, you need to use java.io package to access file methods.