标签:
scala特有的是scala.io.Source,例如:
import scala.io._
Source.fromFile(“cn.scala”,”utf8”).mkString
逐行读文件内容:
Source.fromFile(new java.io.File(“cn.scala”)).getLines().foreach(println)
import java.io., java.nio.channels., java.nio._
// 写文件
val f = new FileOutputStream(“o.txt”).getChannel
f write ByteBuffer.wrap(“a little bit long …”.getBytes)
f close
或者:
var out = new java.io.FileWriter(“./out.txt”) // FileWriter(“./out.txt”, true) 为追加模式
out.write(“hello\n”)
out close
val in = new FileInputStream(“in”).getChannel
val out = new FileOutputStream(“out”).getChannel
in transferTo (0, in.size, out)
‘
import java.net.{URL, URLEncoder} import scala.io.Source.fromURL
fromURL(new URL(“http://www.baidu.com“)).mkString
或者指定编码:
fromURL(new URL(“http://www.baidu.com“))(io.Codec.UTF8).mkString
标签:
原文地址:http://www.cnblogs.com/rain-1/p/5834231.html