原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-17/

There I am,in the world more exciting!

by postbird

www.ptbird.cn



package com.ptbird.scala import java.io.{File, PrintWriter} import scala.io.Source /** * Created by postbird on 2016/5/25. * * 文件的操作 */ object FileTest { def main(args: Array[String]): Unit = { //读取本地的文件 val file = Source.fromFile("E:\\test.txt") for (line <- file.getLines()) { // println(line) // Hello file scala // I am ptbird } file.close() //读取url文件 val fileUrl = Source.fromURL("http://www.ptbird.cn") // fileUrl.foreach(print(_)) // fileUrl.foreach(print) fileUrl.close() //往文件中存储数据 //使用java的文件和读写操作 val writer = new PrintWriter(new File("E:\\test.txt")) for (i <- 1 to 100) { writer.println(i) } writer.close() //从控制台的读取 val line = readLine("input :") println(line) // input :asd // asd } }