JDK-8202055 : Add methods to Files for reading/writing a string from/to a file
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.nio
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-04-19
  • Updated: 2018-10-05
  • Resolved: 2018-06-08
Related Reports
CSR :  
Relates :  

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:

- 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 

The new methods are consistent with the existing ones in terms of not specifying null handling. For the readString���(Path path, Charset cs) method, the max file size that can be read in may be considerably smaller than 2GB given the internal representation of String. Presumably OutOfMemoryError can be thrown in this circumstance. Moving to Approved.

Ah, sorry, my bad. A zip of the specdiff is attached. Thanks!

From an administrative point of view, some persistent and complete representation of the spec change needs to be associated with the request, such as a zip of the specdiff. A link to the specdiff is informative, but not sufficient on its own. Please attach such an artifact and then re-finalize the request.

Thanks for reviewing the CSR. I updated the description.

Overall this is good. I think some updates are in order with respect to the statement, "Currently, users have to rely on third party libraries to get the functionality." That's not really true. One can read a file's contents into a String with String string = new String(Files.readAllBytes(path), cs); and one can write a String to a file with Files.write(path, string.getBytes(cs)); The CSR should be updated with a description of the advantages of the proposed readString() / writeString() methods, which are that they can also use internal implementation techniques to avoid allocating a full, intermediate byte array (in addition to being more convenient).