在Java中,可以使用以下方法来查看字符串的编码格式:
使用getBytes()方法:该方法将字符串转换为字节数组,并返回使用默认编码的字节数组。可以通过指定不同的编码方式来获取对应编码的字节数组。String str = "Hello World!";byte[] bytes = str.getBytes(); // 使用默认编码方式System.out.println(Arrays.toString(bytes));使用String的构造函数:可以使用指定的字符集来创建一个新的字符串对象。如果指定的字符集不支持某些字符,则会使用默认字符集替代。String str = "Hello World!";Charset charset = Charset.forName("UTF-8"); // 指定编码为UTF-8byte[] bytes = str.getBytes(charset);System.out.println(Arrays.toString(bytes));使用Java NIO的CharsetDecoder:可以通过使用指定的字符集来创建一个CharsetDecoder对象,并使用decode()方法解码字符串。import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.nio.charset.CodingErrorAction;import java.nio.ByteBuffer;String str = "Hello World!";CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();decoder.onMalformedInput(CodingErrorAction.IGNORE);decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());String decodedStr = decoder.decode(buffer).toString();System.out.println(decodedStr);以上方法可以帮助你查看字符串的编码格式。注意,在使用getBytes()方法或者String的构造函数时,如果不指定编码方式,默认会使用平台的默认编码方式。