这可能是状态机的一个非常简单的用例。
import collection.mutable.ListBuffercase class Part(contentType:Option[String], encoding:Option[String], location:Option[String], data:ListBuffer[String])var boundary: String = nullval Boundary = """.*boundary="(.*)"""".rvar state = 0val IN_PART = 1val IN_DATA = 2var _contentType:Option[String] = Nonevar _encoding:Option[String] = Nonevar _location:Option[String] = Nonevar _data = new ListBuffer[String]()Source.fromFile("test.mht").getLines.foreach{ case Boundary(b) => boundary = b case `boundary` => _contentType = None _encoding = None _location = None _data = new ListBuffer[String]() state = IN_PART case "" => state match { case IN_PART => state = IN_DATA case IN_DATA => var currentPart = Part(_contentType, _encoding, _location, _data) case _ => } case line => state match { case IN_DATA => _data.append(line) case IN_PART => line.split(":") match { case Array("Content-Type", t) => _contentType = Some(t) case Array("Content-Transfer-Encoding", e) => _encoding = Some(e) case Array("Content-Location", l) => _location = Some(l) case _ => } }}


