此方法使用平台的默认字符集将此String编码为字节序列,并将输出存储到新的字节数组中。
public byte[] getBytes()
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"); } } }
这将产生以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-string-getbytes.html
来源:LearnFk无涯教程网
Returned Value Welcome to Learnfk.com Returned Value Welcome to Learnfk.com
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)