-
Notifications
You must be signed in to change notification settings - Fork 145
File RW
Rajesh edited this page Jun 16, 2024
·
1 revision
Dead simple I/O:
val file = root/"tmp"/"test.txt"
file.overwrite("hello")
file.appendLine().append("world")
assert(file.contentAsString() == "hello\nworld")
If you are someone who likes symbols, then the above code can also be written as:
import better.files.Dsl.SymbolicOperations
file < "hello" // same as file.overwrite("hello")
file << "world" // same as file.appendLines("world")
assert(file! == "hello\nworld")
Or even, right-associatively:
import better.files.Dsl.SymbolicOperations
"hello" `>:` file
"world" >>: file
val bytes: Array[Byte] = file.loadBytes
(root/"tmp"/"diary.txt")
.createIfNotExists()
.appendLine()
.appendLines("My name is", "Inigo Montoya")
.moveToDirectory(home/"Documents")
.renameTo("princess_diary.txt")
.changeExtensionTo(".md")
.lines()