Skip to content

Instantly share code, notes, and snippets.

View lhohan's full-sized avatar

Hans L'Hoëst lhohan

View GitHub Profile
@lhohan
lhohan / new_gist_file
Created September 2, 2013 10:52
helper "use" method using currying for automatic resource management From http://stackoverflow.com/questions/2225214/scala-script-to-copy-files
If you really want to do it yourself instead of using a library like commons-io, you can do the following in version 2.8. Create a helper method "use". It will give you a form of automatic resource management.
def use[T <: { def close(): Unit }](closable: T)(block: T => Unit) {
try {
block(closable)
}
finally {
closable.close()
}
}
@lhohan
lhohan / new_gist_file
Created September 2, 2013 08:39
File copy using Java7 & Scala
Java 7 is now out and you have another option: java.nio.file.Files.copy. The probably easiest solution (And with Scalas superior import even easier). Provided that from and to are strings as in your question:
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files.copy
import java.nio.file.Paths.get
implicit def toPath (filename: String) = get(filename)
copy (from, to, REPLACE_EXISTING)