scala文件读写

作者: xiaogp | 来源:发表于2020-07-04 23:29 被阅读0次
    import java.io.{File, FileWriter, PrintWriter}
    
    import scala.collection.mutable
    import scala.collection.mutable.ListBuffer
    import scala.io.Source
    
    object io_test {
      def main(args: Array[String]): Unit = {
    
        // 获得当前的绝对路径和相对路径
        val file = new File(".")
        println(file.getAbsolutePath)
        println(file.getPath)
    
        test_8()
        test_10()
      }
    
      // 绝对路径读取, 一行一行读
      def test_1(): Unit = {
        val source = Source.fromFile("C:\\Users\\14165\\Desktop\\test\\src\\main\\data\\2017-04-16.txt")
        val lines = source.getLines()
        println(lines.getClass) // class scala.io.BufferedSource$BufferedLineIterator
    
        for (line <- lines) {
          println(line)
          println(line.getClass) // class java.lang.String
        }
        source.close()
      }
    
      // 绝对路径读取, 转化为行列表
      def test_2(): Unit = {
        val source2 = Source.fromFile("C:\\Users\\14165\\Desktop\\test\\src\\main\\data\\2017-04-16.txt")
        val lines = source2.getLines()
        val linesList = lines.toList
        println(linesList.size)
        println(linesList(0))
        println(linesList(0).split(" "))
        source2.close()
    
      }
    
      // 直接读成字符串
      def test_3(): Unit = {
        val source3 = Source.fromFile("C:\\Users\\14165\\Desktop\\test\\src\\main\\data\\2017-04-16.txt")
        val lines3 = source3.mkString
        println(lines3)
        source3.close()
      }
    
      // 相对路径
      def test_4(): Unit = {
        val f = Source.fromFile("src\\main\\resources\\2017-04-16.txt")
        var count = 0
        for (line <- f.getLines()){
          count += 1
        }
        println(count)
      }
    
      // 相对路径读取
      def test_5(): Unit = {
        val f = new File(getClass.getClassLoader.getResource("2017-04-16.txt").getPath)
        val source = Source.fromFile(f, "UTF-8")
        val lines = source.getLines()
        for (line <- lines) {
          println(line)
        }
        source.close()
      }
    
      // 读取加工存储为List
      def test_6(): List[String] = {
        val f = new File(getClass.getClassLoader.getResource("2017-04-16.txt").getPath)
        val source = Source.fromFile(f, "UTF-8")
        val tmpList = ListBuffer[String]()
        for (line <- source.getLines()) {
          tmpList += line
        }
        tmpList.toList
      }
    
      // 读取加工存储为字典
      def test_7(): mutable.HashMap[String, Double] = {
        val f = new File(getClass.getClassLoader.getResource("2017-04-16.txt").getPath)
        val source = Source.fromFile(f, "UTF-8")
        val tmpMap = mutable.HashMap[String, Double]()
        for (line <- source.getLines()) {
          val lineItems = line.split("\t")
          val name = lineItems(0)
          val price = lineItems(1).split("¥")(1).replace(",", "").toDouble
          tmpMap(name) = price
        }
        tmpMap
      }
    
      // 写入文件
      def test_8(): Unit = {
        val out = new PrintWriter(new File("D:\\write_res1.txt"))
        for (i <- 1 to 100) {
          out.println(i)
        }
        out.close()
    
      }
    
      def test_9(): Unit = {
    //    val out = new FileWriter(new File(getClass.getClassLoader.getResource("write_res2.txt").getPath))
        val out = new FileWriter(new File("D:\\write_res2.txt"))
        for (i <- 1 to 10) {
          out.write(i.toString + "\r\n")
        }
        out.close()
      }
    
      def test_10(): Unit = {
        val source = Source.fromFile(new File(getClass.getClassLoader.getResource("2017-04-16.txt").getPath))
        val tmpMap = new mutable.HashMap[String, Double]()
        for (line <- source.getLines()){
          val tmpItem = line.split("\t")
          val name = tmpItem(0).toString
          val price = tmpItem(1).split("¥")(1).replace(",", "").toDouble
          tmpMap(name) = price
        }
        source.close()
    
        val out = new FileWriter(new File("D:\\write_res3.txt"))
        for ((name, price) <- tmpMap) {
          out.write(name + "," + price.toString + "\r\n")
        }
        out.close()
    
      }
    
    def test_11(): Unit = {
        val out = new FileWriter(new File("D:\\write_res2.txt"))
        val source = Source.fromFile(new File(getClass.getClassLoader.getResource("2017-04-16.txt").getPath))
        for (line <- source.getLines()) {
          val name = line.split("\t")(0).toString
          val price = line.split("\t")(1).split("¥")(1).replace(",", "").toDouble
          out.write(name + "," + price + "\r\n")
        }
        source.close()
        out.close()
      }
    
    }
    
    

    相关文章

      网友评论

        本文标题:scala文件读写

        本文链接:https://www.haomeiwen.com/subject/tcfmqktx.html