A DESCRIPTION OF THE REQUEST :
Currently when constructing a java.io.PrintWriter, the charset can only be specified by String. This means two things:
1) I have to catch, or deal with, UnsupportedEncodingException being thrown, and
2) I cannot use the java.nio.charset.StandardCharsets constants to specify the character set that I want to use.
Of course there should be two new constructors:
java.io.PrintWriter.PrintWriter(File, Charset)
java.io.PrintWriter.PrintWriter(String, Charset)
JUSTIFICATION :
Given that I suspect a large portion of the users of this class will just want to write UTF-8, then adding these constructor overloads would make the code simpler. So, instead of:
try {
new PrintWriter(file, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Ignore, this is required to be supported by the JVM.
}
we could write:
new PrintWriter(file, StandardCharsets.UTF_8);
Which gets rid of the boilerplate code to deal with the exception that can never happen.
The effort to implement should be relatively minimal, as the internal constructor already expects a Charset.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That these constructors are added.
ACTUAL -
These constructors have not yet been added.
---------- BEGIN SOURCE ----------
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.io.PrintWriter;
public class Test {
public static int main(String []) {
try {
new PrintWriter("test", StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
// don't mind dealing with this, since it is a real thing
}
}
}
---------- END SOURCE ----------
Currently when constructing a java.io.PrintWriter, the charset can only be specified by String. This means two things:
1) I have to catch, or deal with, UnsupportedEncodingException being thrown, and
2) I cannot use the java.nio.charset.StandardCharsets constants to specify the character set that I want to use.
Of course there should be two new constructors:
java.io.PrintWriter.PrintWriter(File, Charset)
java.io.PrintWriter.PrintWriter(String, Charset)
JUSTIFICATION :
Given that I suspect a large portion of the users of this class will just want to write UTF-8, then adding these constructor overloads would make the code simpler. So, instead of:
try {
new PrintWriter(file, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Ignore, this is required to be supported by the JVM.
}
we could write:
new PrintWriter(file, StandardCharsets.UTF_8);
Which gets rid of the boilerplate code to deal with the exception that can never happen.
The effort to implement should be relatively minimal, as the internal constructor already expects a Charset.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That these constructors are added.
ACTUAL -
These constructors have not yet been added.
---------- BEGIN SOURCE ----------
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.io.PrintWriter;
public class Test {
public static int main(String []) {
try {
new PrintWriter("test", StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
// don't mind dealing with this, since it is a real thing
}
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8183743 Umbrella: add overloads that take a Charset parameter
-
- Resolved
-
- relates to
-
JDK-8183743 Umbrella: add overloads that take a Charset parameter
-
- Resolved
-