Created
November 27, 2010 16:53
-
-
Save tototoshi/718059 to your computer and use it in GitHub Desktop.
scalaから外部プロセスを起動して結果をリストとして受け取る
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
def exec(command:String) = { | |
import java.lang._ | |
import java.io._ | |
def getResult(command:String):Option[BufferedReader] = { | |
try { | |
val is = Runtime.getRuntime().exec(command).getInputStream() | |
Some(new BufferedReader(new InputStreamReader(is))) | |
} catch { | |
case _ => None | |
} | |
} | |
def read(br:BufferedReader):List[String] = br.readLine match { | |
case null => { | |
br.close() | |
Nil | |
} | |
case s => s :: read(br) | |
} | |
getResult(command) match { | |
case Some(br) => read(br) | |
case _ => Nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment