1凄吏、讀取網(wǎng)絡(luò)資源
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远舅、寫入數(shù)據(jù)到文件
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
}
}
在特質(zhì)中,我們使用過PrintStream竞思,寫過文件
3表谊、控制臺操作
(1)老API
object ConsoleWriter {
def main(args: Array[String]): Unit = {
//控制臺交互--老API
print("請輸入內(nèi)容:")
val consoleLine1 = Console.readLine()
println("剛才輸入的內(nèi)容是:" + consoleLine1)
}
}
(2)新API
import scala.io.StdIn
object ConsoleWriter {
def main(args: Array[String]): Unit = {
//控制臺交互--新API
print("請輸入內(nèi)容(新API):")
val consoleLine2 = StdIn.readLine()
println("剛才輸入的內(nèi)容是:" + consoleLine2)
}
}