You're doing it wrong; a String
is a set of characters and that is all. What you do here is:
- you get the bytes of the string as decoded by your current JVM encoding,
- you reencode these bytes using a different encoding.
Your string will therefore be completely corrupted. A String
does not have an encoding.
See here for more details.
As to UCS-2, it has been superseded by UTF-16. You want to use UTF-16 instead.
Note that you MUST specify the endianness, which matters for UTF-16 unlike for UTF-8. Use:
StandardCharsets.UTF_16LE
(or BE
for big endian), or, if you still use Java 6 or lower:
Charset.forName("UTF-16LE") // or BE