zoukankan      html  css  js  c++  java
  • Scala文件操作

    本文记录了Scala对文件的操作,主要包括遍历文件、文件复制、递归读取文件、序列化和反序列化。

    import java.io._
    
    import scala.io.Source
    
    object FileOperation {
    
        def main(args: Array[String]): Unit = {
    
            // 遍历文件的每一行
            // 01 使用source.getLines()返回的迭代器
            val source01 = Source.fromFile("D:/test.txt", "UTF-8")
            val lineIterator01 = source01.getLines()
            for (line <- lineIterator01) println(line)
    
            // 02 将Source.getLines返回的迭代器,转换成数组
            val source02 = Source.fromFile("D:/test.txt", "UTF-8")
            val lineIterator02 = source02.getLines().toArray
            for (line <- lineIterator02) println(line)
    
            // 03 调用Source.mkString,返回文本中所有的内容
            val source03 = Source.fromFile("D:/test.txt", "UTF-8")
            println(source03.mkString)
            source03.close()
    
            // 04 遍历文件中的每个字符
            val source04 = Source.fromFile("D:/test.txt", "UTF-8")
            for (c <- source04) println(c + " ")
    
            // 05 从URL中读取字符
            val source05 = Source.fromURL("https://www.hao123.com", "UTF-8")
            for (c <- source05) print(c + " ")
    
            // 06 从字符串中读取
            val source06 = Source.fromString("Hello World")
            for (c <- source06) print(c + " ")
    
            // 07 文件copy
            val in = new FileInputStream(new File("D:/test.txt"))
            val out = new FileOutputStream(new File("D:/test_copy.txt"))
            val buf = new Array[Byte](1024)
            in.read(buf)
            out.write(buf, 0, 1024)
            in.close()
            out.close()
    
            // 08 写文件
            val pw = new PrintWriter("D:/test_write.txt")
            pw.println("hello world")
            pw.close()
    
            // 09 递归遍历子目录
            val iterator = recursion(new File("D:/test"))
            for (dir <- iterator) println(dir)
    
            // 序列化
            val angus = new P("angus")
            val oos = new ObjectOutputStream(new FileOutputStream("D:/test/angus.obj"))
            oos.writeObject(angus)
            oos.close()
    
            // 反序列化
            val ois = new ObjectInputStream(new FileInputStream("D:/test/angus.obj"))
            val restoredAngus = ois.readObject().asInstanceOf[P]
            println(restoredAngus.name)
        }
    
    
        def recursion(dir: File): Iterator[File] = {
            val childDirs = dir.listFiles.filter(_.isDirectory)
            childDirs.toIterator ++ childDirs.toIterator.flatMap(recursion)
        }
    
    
        class P(val name: String) extends Serializable
    
    
    }
  • 相关阅读:
    leapftp 注册码大全
    一个虚拟主机上放多个网站(asp.net)
    服务器部署VS 2005/2008 ReportViewer,完美支持中文
    ASPNET项目打包时遇到错误:无法生成项目输出组内容文件来自Web(活动)
    Internet Explorer cannot open the Internet site Operation aborted
    Creating subprojects in IIS with Web Application Projects
    Microsoft Chart Control vs. Dundas Chart Control
    RDLC 示例 文章 1
    WSIT联接WCF
    一个毕业6年的程序员工作经历和成长历程(中2)
  • 原文地址:https://www.cnblogs.com/dreamfor123/p/9337164.html
Copyright © 2011-2022 走看看