Java 以编程方式检查是否支持编码

Java 以编程方式检查是否支持编码,java,encoding,character-encoding,Java,Encoding,Character Encoding,有没有办法确定是否支持编码?例如。这样的方法: isSupported("UTF-8") == true 及 我需要这个来验证我的mimessages的Content-Disposition头是否正确 boolean isCharsetSupported(String name) { try { Charset.forName(name); return true; } catch (UnsupportedCharsetException | IllegalCharse

有没有办法确定是否支持编码?例如。这样的方法:

isSupported("UTF-8") == true

我需要这个来验证我的mimessages的Content-Disposition头是否正确

boolean isCharsetSupported(String name) {
  try {
    Charset.forName(name);
    return true;
  } catch (UnsupportedCharsetException | IllegalCharsetNameException | IllegalArgumentException e) {
    return false;
  }
}
或者没有
try/catch
块:

boolean isCharsetSupported(String name) {
    return Charset.availableCharsets().keySet().contains(name);
}
请尝试以下操作:

Charset.isSupported("UTF-8")

当名称为
null
或名称无效时,此方法可能会抛出
RuntimeException
s。

我希望没有异常,但无论如何还是要感谢;)在Java中不鼓励使用
Exception
s作为流控制。。。但是,如果给定的字符集名称是illegal@jlordo正如我写的,当名称无效时,它可能会抛出一个
运行时异常。
Charset.isSupported("UTF-8")