输入输出

输入

scala> import scala.io.StdIn._
import scala.io.StdIn._

scala> val a = read
readBoolean   readByte   readChar   readDouble   readFloat   readInt   readLine   readLong   readShort

scala> val a = readInt()
a: Int = 1212

格式化输出

scala> val i = 5
i: Int = 5

scala> val f = 2.1
f: Double = 2.1

scala> val s = "Hello"
s: String = Hello

scala> println(s"$s:i=$i,f=$f")
Hello:i=5,f=2.1

scala> println(s"$ss:i=$i,f=$f")
<console>:17: error: not found: value ss
       println(s"$ss:i=$i,f=$f")
                  ^

scala> println(f"$s:i=$i,f=$f%.2f")
Hello:i=5,f=2.10

输出到文件

import java.io.PrintWriter

val outPutFile =  new PrintWriter("D:\\大数据技\\Spark_scala\\scala\\data\\outData.txt")
outPutFile.println("outPut data!")
outPutFile.close()

读取文件

import scala.io.Source

val inPutFile = Source.fromFile("D:\\大数据技\\Spark_scala\\scala\\data\\readData.txt")
val textIter = inPutFile.getLines()
for(info <- textIter){
    println(info)
}
posted @ 2019-08-30 06:15  会走的树  阅读(111)  评论(0编辑  收藏  举报