If you want to find out if the specified string is supported by the target character code, it is convenient to use
CharsetEncoder.canEncode. This is a method that returns true if the method given as an argument is supported by the target character code, false otherwise, for example, if you want to check whether the string
s is supported in Shift_JIS, the following You can write as follows.
String s = "..."; Charset.forName("Shift_JIS").newEncoder().canEncode(s);
As an example of verifying that it is actually working, we will use the kanji "Taka". It's a so-called "ladder". This Kanji is not supported in Shift_JIS, but it is supported in its extension Windows-31J. Let's check this.
Charset.forName("Shift_JIS").newEncoder().canEncode("Taka"); //=> false Charset.forName("Windows-31J").newEncoder().canEncode("Taka"); //=> true
It seems to be working as expected (´ ・ ω ・ ｀)
** Environmental Information: **
C:\>javac -version javac 11.0.3 C:\>java -version openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.3+7) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.3+7, mixed mode)