Skip to content

Instantly share code, notes, and snippets.

@ntakk
Created June 18, 2012 15:09
Show Gist options
  • Select an option

  • Save ntakk/2948828 to your computer and use it in GitHub Desktop.

Select an option

Save ntakk/2948828 to your computer and use it in GitHub Desktop.
loan pattern sample2
package com.github.takuya71
import java.io._
object Loanp {
def main(args: Array[String]) {
writeFile(new File("test.txt")){writer =>
{
//write start
writer.println("line:1")
writer.println("line:2")
//write end
}
}
}
def writeFile[T](file: File)( write: PrintWriter => T):T = {
//open file
val writer = new PrintWriter(file)
try {
write(writer)
} finally {
//close file
writer.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment