Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created November 27, 2010 16:53
Show Gist options
  • Save tototoshi/718059 to your computer and use it in GitHub Desktop.
Save tototoshi/718059 to your computer and use it in GitHub Desktop.
scalaから外部プロセスを起動して結果をリストとして受け取る
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