Scala中的文件操作基本可以依赖于Java的实现,包括输入、输出流的使用。
object FileOps {
def main(args: Array[String]) {
val file = Source.fromFile("E:\\WangJialin.txt")
for (line <- file.getLines){ println(line)}
file.close
val webFile = Source.fromURL("http://spark.apache.org/")
webFile.foreach(print)
webFile.close
val writer = new PrintWriter(new File("scalaFile.txt" )) //写数据一般需要调用Java api
for (i <- 1 to 100) writer.println(i)
writer.close()
print("Please enter your input : " )
val line = readLine
println("Thanks, you just typed: " + line)
val file=Source.fromFile("E:\\hehe.txt")
for(line<-file.getLines()){println(line)}
file.close();
}
}
时间: 2024-11-10 04:59:24