目录

byte getBytes()

描述 (Description)

此方法使用平台的默认字符集将此String编码为字节序列,并将结果存储到新的字节数组中。

语法 (Syntax)

以下是此方法的语法 -

public byte[] getBytes()

返回值 (Return Value)

  • 此方法返回结果字节数组。

例子 (Example)

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to iowiki.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");
      }
   }
}

这将产生以下结果 -

输出 (Output)

Returned Value Welcome to iowiki.com
Returned Value Welcome to iowiki.com
↑回到顶部↑
WIKI教程 @2018