Mika'il Mika'il - 29 days ago 21
Scala Question

No output when reading file

I'm using Akka FileIO to read a file line by line as follows:

def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("MySys")
implicit val materializer = ActorMaterializer()
FileIO.fromPath(Paths.get("/foo/1.csv")).via(Framing.delimiter(ByteString("\n"), 256, true).map(_.utf8String)).runForeach(println)
system.terminate()
}


But when I run this, I see no output on the console. The file path is correct and there are contents in the file. What am I doing wrong?

Answer

You terminate AkkaSystem before it has opportunity to process your file.

Do something like this

FileIO.fromPath(Paths.get("/foo/1.csv"))
  .via {
    Framing.delimiter(ByteString("\n"), 256, true).map(_.utf8String)
  }
  .runForeach(println)
  .onComplete{ _ =>
    system.terminate()
  }
Comments