此方法使用命名的字符集将此String编码为字节序列,并将输出存储到新的字节数组中。
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
这是参数的详细信息-
charsetName - 支持的字符集的名称。
import java.io.*; public class Test { public static void main(String args[]) { String Str1=new String("Welcome to Learnfk.com"); try { String Str2=new String(Str1.getBytes( "UTF-8" )); System.out.println("Returned Value " + Str2 ); Str2=new String (Str1.getBytes( "ISO-8859-1" )); System.out.println("Returned Value " + Str2 ); } catch ( UnsupportedEncodingException e) { System.out.println("Unsupported character set"); } } }
这将产生以下输出-
Returned Value Welcome to Learnfk.com Returned Value Welcome to Learnfk.com
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)