File Locking And Block Execution

Examples using file locks and executing multiple operations within the context of a single open file

Operation Block

when several operation need to be performed on a file it is often more performant to execute them within an function passed to the open method this is because the underlying filesystem has options for optimizing the use of the file channels for example a file could be mapped into memory for the duration of the function and all operations could be performed using the same channel object
    import scalax.file.{FileOps, Path}
    // see codec examples in scala io core for details on why there is an implicit codec here
    implicit val codec = scalax.io.Codec.UTF8

    val file: FileOps =  Path ("file")

    file.open()( f => {
      val s = f.slurpString
      file.write(s.replaceAll("l", "L"))
    })