1、读取网络资源
import scala.io.Source
object Filebd {
def main(args: Array[String]): Unit = {
val webFile = Source.fromURL("http://www.baidu.com","UTF-8")
webFile.getLines.foreach(println)
webFile.close()
}
}
2、写入数据到文件
object FileWriter {
def main(args: Array[String]): Unit = {
import java.io.{File => JavaFile,PrintWriter => JavaPrintWriter}
val strPath = "File.txt"
val writer = new JavaPrintWriter(new JavaFile(strPath))
for (i <- 1 to 100){
writer.println(i)
}
writer.close
}
}
在特质中,我们使用过PrintStream,写过文件
3、控制台操作
(1)老API
object ConsoleWriter {
def main(args: Array[String]): Unit = {
//控制台交互--老API
print("请输入内容:")
val consoleLine1 = Console.readLine()
println("刚才输入的内容是:" + consoleLine1)
}
}
(2)新API
import scala.io.StdIn
object ConsoleWriter {
def main(args: Array[String]): Unit = {
//控制台交互--新API
print("请输入内容(新API):")
val consoleLine2 = StdIn.readLine()
println("刚才输入的内容是:" + consoleLine2)
}
}