Scala File I/O
-- Learn not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Scala Tutorial
Scala TutorialScala IntroductionScala Installation and Environment ConfigurationScala Basic SyntaxScala Data TypesScala Literals Scala Escape Characters Scala VariablesScala Access ModifiersScala OperatorsScala IF...ELSE StatementsScala LoopsScala Methods and FunctionsScala ClosuresScala StringsScala ArraysScala CollectionsScala IteratorsScala Classes and ObjectsScala TraitsScala Pattern MatchingScala Regular ExpressionsScala Exception HandlingScala ExtractorsScala File I/O
Scala File I/O
For file write operations in Scala, the I/O classes from Java (java.io.File) are used directly:
Example
import java.io._
object Test {
def main(args: Array){
val writer =new PrintWriter(new File("test.txt"))
writer.write("")
writer.close()
}
}
Executing the above code will generate a test.txt file in your current directory, with the file content as "":
$ scalac Test.scala $ scala Test $ cat test.txt
YouTip