JDK-8198790 : Add constructors with Charset parameter for FileReader and FileWriter
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-02-27
  • Updated: 2018-03-14
  • Resolved: 2018-03-14
Related Reports
CSR :  
Description
Summary
-------

Add constructors for the FileReader and FileWriter that take a
Charset parameter.

Problem
-------

The FileReader and FileWriter were designed as a convenient API for 
situations where default settings are desirable. However, that assumption
was flawed since assuming the platform's default Charset can often cause
confusion and lead to error-prone code in situations where the default
Charset is not appropriate.

Solution
--------

Add constructors that explicitly take a Charset parameter. This solution 
provides developers with a means to specify the Charset, as well as
a clear API indicating that a Charset shall be specified in most cases.


Specification
-------------

Below is a summary of the proposed changes. The specdiff zip file is attached. 
It can be viewed at:

http://cr.openjdk.java.net/~joehw/jdk11/8183554/specdiff/overview-summary.html

- java/io/FileReader

>public FileReader���(String fileName, Charset charset) throws FileNotFoundException

>public FileReader���(File file, Charset charset) throws FileNotFoundException


- java/io/FileWriter

>public FileWriter���(String fileName, Charset charset) throws IOException

>public FileWriter���(String fileName, Charset charset, boolean append) throws IOException

>public FileWriter���(File file, Charset charset) throws IOException

>public FileWriter���(File file, Charset charset, boolean append) throws IOException


Comments
Moving to Approved.
14-03-2018

Done. Thanks! I didn't realize I needed to move it back.
14-03-2018

Please move the CSR back to Finalized state it restart its review.
14-03-2018

FileReader(FileDescriptor,Charset) and FileWriter(FileDescriptor,Charset) were deliberately dropped from the proposal, the reason is that the existing methods are highly problematic (you can create a FileReader with a FileDescriptor that was created for writing to mention just one of many issues). We also don't expect average developers to be using java.io.FileDescriptor directly as it tends to be used more with libraries that have native code. On the parameter ordering, I agree with the append parameter being the last parameter. If we ever add FileWriter(String, Charset, OpenOption...) then it would be consistent as the options to configure how the file is created or opened are at the end.
07-03-2018

Yes, the work is for JDK 11. Thanks for updating that. FileDescriptor was left out because it is known to be problematic and we'd like to discourage using it. A FileReader or FileWriter created with a FileDescriptor shares a reference with the InputStream or OutputStream that owns it, which could introduce an issue that is hard to find because both may close it. Refer to the open review thread at http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-March/051910.html. For the order of parameters, yes, we intentionally moved Charset to before the boolean append to emphasize the importance of specifying a Charset. It is true though that the extra parameter is expected to be behind the existing one.
07-03-2018

I assume this work is intended for JDK 11 and I'm updating the CSR accordingly. Is there a reason the java.io.FileDescriptor constructors don't get overloads with a Charset parameter? Too low of a duty-cycle? Should the new string & charset & boolean constructor be FileWriter���(java.lang.String fileName, Charset charset, boolean append) (as currently proposed) or FileWriter���(java.lang.String fileName, boolean append, Charset charset) In the other constructors added, the charset is the last parameter. Procedurally marking the issue as pended until these questions are resolved.
07-03-2018