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

Add methods to Files for reading/writing a string from/to a file

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 11
    • core-libs
    • None
    • minimal
    • Hide
      There is no compatibility risk. The proposed methods are new to Files.
      Show
      There is no compatibility risk. The proposed methods are new to Files.
    • Java API
    • SE

      Summary

      Add methods to java.nio.file.Files for reading a file into a string and writing a string to a file.

      Problem

      Reading and writing a string from and to a file are common File operations. Currently, java.nio.file.Files already provides methods for the conversion between a byte array and file, as well as a list and file. An expansion to a string and file transformation would make it more complete for the transformation between data types and files.

      Adding string-to-file APIs gives an option for a convenient and direct conversion between a string and file. While applications can already use the byte array-to-file APIs to indirectly read and write strings, a direct string-to-file API provides an opportunity for the implementation to optimize the process, making the operation more efficient.

      Solution

      Add a method readString(Path), along with a Charset overload; Add a method write(Path, String), along with a Charset overload;

      Specification

      Below is a summary of the proposed changes. The specdiff can be viewed at: http://cr.openjdk.java.net/~joehw/jdk11/8201276/specdiff/java/nio/file/Files.html

      • java.nio.file.Files

      public static String readString(Path path) throws IOException

      public static String readString(Path path, Charset cs) throws IOException

      public static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException

      public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options) throws IOException

            joehw Joe Wang
            joehw Joe Wang
            Alan Bateman, Roger Riggs, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: