System.in 및 File IO

scala 2016. 3. 7. 11:13

1. io.Source.stdin 이용    REF-SITE

   for (ln <- io.Source.stdin.getLines) println(ln)  

   (참고: break나 return이 잘 안되서 study 중 )


  1.5 위에서 stdin = fromInputStream(System.in)으로 def(ine)되어 있다. 따라서 아래처럼 변형도 가능

    val input = Source.fromInputStream(System.in);

   val lines = input.getLines.collect


2. System.in 에서 읽기

=> readLine()함수가 system.in에서 바로 라인을 읽는다.

끝나면 null을 리턴하므로 체크 필요.

object SysteminTest {
  def main(args: Array[String]) {

var line = true
do {
var str = readLine() //depricated..
if (str!=null) println(str)

else line=false //need test..
} while ( line )

} }



2. File에서 읽기

Line을 읽어서 String으로 만드는데,  empty Line을 공백" " 으로 처리하는 방법.

val file = Source.fromFile(args(0)).getLines().filter(!_.isEmpty()).mkString(" ")

Posted by yongary
,