Add methods to java.nio.file.Files for reading a file into a string and writing a string to a file.
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.
Add a method readString(Path), along with a Charset overload; Add a method write(Path, String), along with a Charset overload;
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
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
- csr of
JDK-8201276 (fs) Add methods to Files for reading/writing a string from/to a file
- relates to
JDK-8211773 JDK 11 GA fails JCK11 test: UTF-8/UTF-16 String data conversion problem