-
Notifications
You must be signed in to change notification settings - Fork 145
Instantiation
Rajesh edited this page Jun 16, 2024
·
1 revision
Recipes to create a File
import better.files._
import File._
import java.io.{File => JFile}
val f = File("/User/johndoe/Documents") // using constructor
val f1: File = file"/User/johndoe/Documents" // using string interpolator
val f2: File = "/User/johndoe/Documents".toFile // convert a string path to a file
val f3: File = new JFile("/User/johndoe/Documents").toScala // convert a Java file to Scala
val f4: File = root/"User"/"johndoe"/"Documents" // using root helper to start from root
val f5: File = `~` / "Documents" // also equivalent to `home / "Documents"`
val f6: File = "/User"/"johndoe"/"Documents" // using file separator DSL
val f7: File = "/User"/'johndoe/'Documents // same as above but using Symbols instead of Strings
val f8: File = home/"Documents"/"presentations"/`..` // use `..` to navigate up to parent
Note: Rename the import if you think the usage of the class File
may confuse your teammates:
import better.files.{File => ScalaFile, _}
import java.io.File
I personally prefer renaming the Java crap instead:
import better.files._
import java.io.{File => JFile}