==> 讀取行ide
// 讀取行 val source = scala.oi.Source.fromFile("d:\\test\\a.txt") // 將整個文件做爲一個字符串 // 將文件中的每一行讀入
==> 讀取字符spa
val source = scala.oi.Source.fromFile("d:\\test\\a.txt") for(c <- source) println(c)
==> 從 URL 或其它源讀取: 須要指定字符集 UTF-8scala
val source = scala.oi.Source.fromURL(" "UTF-8" ) println(source.mkString)
==> 讀取二進制文件(Scala 不支持直接讀取二進制文件,可是能夠經過調用 Java 的 InputStream 來進行讀入)orm
// 讀取二進制文件 val file = new File("d:\\test\\test.war") // 構造一個 InputStream val in = new FileInputStream(file) // 構造一個 buffer val buffer = new Array[Byte](file.length().toInt) // 讀取 in.read(buffer) // 關閉 in.close()
==> 寫入文本文件字符串
val out = new PrintWriter("d:\\test\\test.txt") for(i <- 1 to 20) out.println(i) out.close()