chapter09

  import java.io.File
import java.nio.file._

import scala.collection.mutable.ArrayBuffer
/**
* Created by EX-CHENZECHAO001 on 2018-04-02.
*/
class Chapter09 {
// 9.7 访问目录
def subdirs(dir: File): Iterator[File] = {
val children = dir.listFiles().filter(_.isDirectory)
children.toIterator ++ children.toIterator.flatMap(subdirs(_))
}
implicit def makeFileVisitor(f: (Path) => Unit) = new SimpleFileVisitor[Path] {

}
}




object Chapter09 extends App{
val files = new Chapter09().subdirs(new File("D:\chenzechao\_learn"))
for(file <- files){
println(file.getName)
}
}

// 9.8 序列化
class Person98 extends Serializable {

}
// 如果接受缺省ID,可略去@SerialVersionUID
// 按常规方式对对象进行序列化和反序列化
object Person98 {
val fred = new Person98()
import java.io._
val out = new ObjectOutputStream(new FileOutputStream("/tmp/abc.obj"))
out.writeObject(fred)
out.close()
val in = new ObjectInputStream(new FileInputStream("/tmp/abc.obj"))
val saveFred = in.readObject().asInstanceOf[Person98]

// Scala集合类都是可序列化的
class Person98_01 extends Serializable{
private val friends = new ArrayBuffer[Person98]
}
}

// 9.8 进程控制
import sys.process._
// "ls -al .."
// sys.process包包含一个从字符器到ProcessBuilder对象的隐式转换。!操作符执行的就是这个ProcessBuilder对象
// !操作符返回的被执行程序的返回值:程序成功执行的话就是0,否则就是显示错误的非0值
// 如果使用!!则返回命令执行结果
val result = "ls -al .." !!
原文地址:https://www.cnblogs.com/chenzechao/p/8696026.html