JDK-8183554 : Add constructors with Charset parameter for FileReader and FileWriter
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2017-07-04
  • Updated: 2021-04-24
  • Resolved: 2018-03-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11 b05Fixed
Related Reports
CSR :  
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
A FileReader constructor should take a Charset.

JUSTIFICATION :
Currently you have to use an InputStreamReader wrapping a FileInputStream to put in a Charset.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
FileReader fr = new FileReader( file, charset );
ACTUAL -
Reader r = new InputStreamReader( new FileInputStream( file ), charset );

CUSTOMER SUBMITTED WORKAROUND :
Reader r = new InputStreamReader( new FileInputStream( file ), charset );


Comments
Reopening this RFE. It's true that there are workarounds, such as using InputStreamReader or nio Files.newBufferedReader. (And corresponding alternatives for output.) However, having these APIs here continues to be an "attractive nuisance" even for new code, as it's a recurring mistake for developers to use these classes even though the default character set is NOT satisfactory for their application. This is simply too easy a mistake to make. I think the charset-bearing overloads should be added. Eventually the default-charset-using overloads might end up being deprecated, and having the charset-bearing overloads here makes the transition easier. The mentions in the specs of the default charset should simply be removed.
01-02-2018

The specification for FileReader stated that "The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable", that indicated that FileReader was designed as a convenient API for situations where default settings are desirable. It recommended constructing an InputStreamReader on a FileInputStream when a specific charset is needed. With java.nio, users shall consider Files::newBufferedReader���(Path path, Charset cs).
02-12-2017