How to ensure valid path object is returned in Java

59 views Asked by At

I am going through the quickstart guide in the Akka Streams docs for Scala and in one of the examples they are using Akka Streams to write to a file.

import java.nio.file.Paths

import scala.concurrent.*

import akka.*
import akka.actor.ActorSystem
import akka.stream.*
import akka.stream.scaladsl.*
import akka.util.ByteString
import java.nio.file.Paths

object StreamsQuickstart:
  def main(args: Array[String]): Unit =
    implicit val system: ActorSystem = ActorSystem("Quickstart")
    val source: Source[Int, NotUsed] = Source(1 to 100)

    // val done: Future[Done] = source.runForeach(println)

    val factorials = source.scan(BigInt(1))((acc, next) => acc * next)
    val result: Future[IOResult] =
      factorials.map(num => ByteString(s"$num\n")).runWith(FileIO.toPath(Paths.get("factorials.txt")))

This is the code from the Akka docs, but it won't compile since Paths.get("factorials.txt") returns a java.nio.file.Path | Null but FileIO.toPath only accepts a Path object as an argument. How can I ensure that a Path is returned from Paths.get?

I looked through the docs for Paths but I didn't see anything about ensuring that a non Null object is returned. Being from the Akka docs, the least I would expect is that code like this would compile.

0

There are 0 answers