Write to file from endless stream

Heys guys,

I have a endless streams (a consumer from a queue), that append each element to a file (file path depends on meta data).

source.via(business()).via(saveToFile()).via(business())

Of course, I could write for each a element a Flow, but thats quite slow approach, right?

Source.single(elem).via(toByteString()).runWith(FileIO.toPath(...),system)

Is there a faster solution to append these elements to a file?

Alpakka has a file rotator, but this will only be optimal for you, if the elements not really alternate (bcs it meant to rotate files completely, and not keeping the substreams (and files) open).

If the files you need to write to are alternating (like a.txt, b.txt, a.txt, b.txt), I think you should modify the LogRotatorSink to keep N substreams open, sorted by the last usage.

Yes, I built now a flow that writes the file, because the files don’t rotate.