1.scala往文件里面写,如果没有该文件,会新建这个文件。
package com.iflytek.file
import java.io._
object FileWrite {
def main(args: Array[String]) {
val writer = new PrintWriter(new File("e:\\test.txt" ))
writer.write("hello,world")
writer.close()
}
}
2.scala从文件里面读取内容
package com.iflytek.file
import scala.io.Source
object FileRead {
def main(args: Array[String]) {
println("文件内容为:" )
Source.fromFile("e:\\test.txt" ).foreach{
print
}
}
}
3.sacla从控制台获取用户输入的信息
package com.iflytek.file
object Test {
def main(args: Array[String]): Unit = {
println("请输入你想输入的内容:")
while(true){
val content=Console.readLine()
println("你輸入的内容是:"+content)
}
}
}