Description
There are methods on Files to obtain a Stream of lines from a file, but there is no counter part to write a Stream lines.
There exist methods to write an Iterable of lines:
public static Path write(Path path, Iterable<? extends CharSequence> lines,
Charset cs, OpenOption... options)
throws IOException
public static Path write(Path path,
Iterable<? extends CharSequence> lines,
OpenOption... options)
throws IOException
A Stream can be converted to an Iterable using s::iterator. Stream.iterator() is considered more of an escape hatch (and there are also some awkward edge cases for target type matching that require a cast for a method reference to be used directly with the above write methods).
Similar methods can be added that accept Stream<? extends CharSequence>. Overloads can probably be used, since a class implementing Stream and Iterable is arguably confused in the contract it supports for traversal. Alternatively a different method name can be used, such as writeLines.
There exist methods to write an Iterable of lines:
public static Path write(Path path, Iterable<? extends CharSequence> lines,
Charset cs, OpenOption... options)
throws IOException
public static Path write(Path path,
Iterable<? extends CharSequence> lines,
OpenOption... options)
throws IOException
A Stream can be converted to an Iterable using s::iterator. Stream.iterator() is considered more of an escape hatch (and there are also some awkward edge cases for target type matching that require a cast for a method reference to be used directly with the above write methods).
Similar methods can be added that accept Stream<? extends CharSequence>. Overloads can probably be used, since a class implementing Stream and Iterable is arguably confused in the contract it supports for traversal. Alternatively a different method name can be used, such as writeLines.