1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import java.io._, java.nio._
object IODemo{ def main(args: Array[String]): Unit = { val path = "D:\\data\\oo.txt" netIo }
def read(path:String)= { import scala.io._
println(Source.fromFile(path, "utf8").mkString) Source.fromFile(new java.io.File(path)).getLines().foreach(println) }
def write(path:String)={ val f = new FileOutputStream(path).getChannel f write ByteBuffer.wrap("a little bit long ...".getBytes) f close var out = new java.io.FileWriter(path) out.write("hello\n") out close }
def copy={ val in_path = "D:\\data\\oo.txt" val out_path = "D:\\data\\oo_copy.txt" val in = new FileInputStream(in_path).getChannel val out = new FileOutputStream(out_path).getChannel in transferTo (0, in.size, out) }
def netIo: Unit ={ import java.net.{URL, URLEncoder} import scala.io.Source.fromURL
println(fromURL(new URL("https://www.baidu.com")).mkString) println(fromURL(new URL("https://www.baidu.com"))(io.Codec.UTF8).mkString) }
}
|