Created
June 18, 2012 15:09
-
-
Save ntakk/2948828 to your computer and use it in GitHub Desktop.
loan pattern sample2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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