Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8141608

Add a Files.write that accepts a Stream of lines

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Unresolved
    • P4
    • None
    • 9
    • core-libs

    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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            psandoz Paul Sandoz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: