目录


1. 读

Source.fromFile

可以读取源包括:File,String,URI,并且可以指定编码和读取文件的长度。

Scala IO_转换流

Source.fromFile 读取的数据也可以转换为很多方式。

Scala IO_转换流_02

按行读取

import scala.io.Source

object TestRead {
  def main(args: Array[String]): Unit = {

    val source = Source.fromFile("d://data/word.txt", "utf-8")
    val lineIterator = source.getLines
    while (lineIterator.hasNext) {
      val line = lineIterator.next()
      println(line)
    }

    source.close
  }
}

// IMEI,356145080566857
// IDFA,1E2DFA89-496A-47FD-9941-DF1FC4E6484A

读取为字符

    val source = Source.fromFile("d://data//word.txt")

    val byteIterator = source.buffered
    while (byteIterator.hasNext) {
      val byte = byteIterator.next
      println(byte)
    }

    source.close

读取为字符串

    val source = Source.fromFile("d://data//word.txt")

    val strArr = source.mkString.split("\n")
    for(str <- strArr) println(str)

    // 读取到数组
    val strArray = for(str <- strArr) yield str
    strArray.foreach(println)

    source.close

Source读取其他源

    // 读取字符
    val byteArray = Array[Byte]('1','a')
    Source.fromBytes(byteArray)

    // 读取流
    val inputStream:InputStream = new FileInputStream("d://data/word.txt")
    Source.fromInputStream(inputStream)

    // 读取URL
    val url = new URL("http://baidu.com")
    val sourceUrl = Source.fromURL(url)
    sourceUrl.foreach(print)

    // 读取URI
    val uri = new URI(...)
    val sourceUri = Source.fromURI(uri)

    //source.close

Java API

InputStream 以字节流方式读取,读入的是一个字节(二进制),组成 byte 数组(byte:8位)。Reader 将文件以字符流方式读入,读入的是 char数组(char:16位,Unicode编码)或 String。
InputStreamReader 是 Reader 和 Stream 之间的转换流,可以将 Stream 转换成字符流方式,可以指定转换编码。

FileInputStream

    // val file = new File("d://data//word.txt")
    // val inputStream = new FileInputStream(file)                         // 流,二进制读取
    // val inputStreamReader = new InputStreamReader(inputStream,"utf8")   // 转换流为Reader
    // val bufferedReader = new BufferedReader(inputStreamReader)          // 转换为BufferReader

    // 通常 FileInputStream和 InputStreamReader对象只使用一次,所以不定义对象
    val bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("d://data//word.txt")))

    var result = ""
    result += Stream.continually(bufferedReader.readLine()).takeWhile(_ != null).mkString("\n")
    println(result)

    bufferedReader.close()

FileReader

    // val file = new File("d://data//word.txt")
    // val fileReader = new FileReader(file)
    // val bufferedReader = new BufferedReader(fileReader)

    // 如果 InputStreamReader和FileReader 对象只使用一次,不定义对象
    val bufferedReader = new BufferedReader(new FileReader("d://data//word.txt"))

    var result = ""
    result += Stream.continually(bufferedReader.readLine()).takeWhile(_ != null).mkString("\n")
    println(result)

    bufferedReader.close()
  2. 写

FileWrite 

import java.io.{File, FileWriter}

object TestWrite {
  def main(args: Array[String]): Unit = {
    val file = new File("d://data//out.txt")
    var fileWriter = new FileWriter(file)

    val source = Source.fromFile("d://data//word.txt")
    val lineIterator = source.getLines()
    while(lineIterator.hasNext){
      val line = lineIterator.next()
      fileWriter.write(line+"\n")
      fileWriter.flush()
    }
    fileWriter.close()
    source.close()
  }
}

PrintWriter 

    import java.io.{File,PrintWriter}

    val file = new File("d://data//out.txt")
    val out = new PrintWriter(file)

    val source = Source.fromFile("d://data//word.txt")
    val lineIterator = source.getLines()
    while (lineIterator.hasNext) {
      val line = lineIterator.next()
      out.write(line + "\n")
      out.flush()
    }
    out.close()
    source.close()

RandomAccessFile

    val file = new File("d://data//out.log")
    val accessFile = new RandomAccessFile(file,"rw")    // r 读,w 写
    accessFile.seek(accessFile.length())                // 指向文件末尾

    val source = Source.fromFile("d://data//word.txt")
    val lineIterator = source.getLines()
    while (lineIterator.hasNext) {
      val line = lineIterator.next()
      accessFile.write(line.getBytes())
      accessFile.write('\n')
    }
    accessFile.close()
    source.close()