集册 Java实例教程 通过转换字符串文本创建UTF8文本

通过转换字符串文本创建UTF8文本

欢马劈雪     最近更新时间:2020-01-02 10:19:05

554
通过转换字符串文本创建UTF8文本


import java.io.UnsupportedEncodingException;

import java.util.Arrays;
//时代Java公众号 - N o w J a  v a . c o m 提供

public class StringConverter {


  public static void main(String[] args) {


    System.out.println(System.getProperty("file.encoding"));

    String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");


    System.out.println("original = " + original);

    System.out.println();


    try {

      byte[] utf8Bytes = original.getBytes("UTF8");

      byte[] defaultBytes = original.getBytes();


      String roundTrip = new String(utf8Bytes, "UTF8");

      System.out.println("roundTrip = " + roundTrip);


      System.out.println();//nowjava.com - 时  代  Java 提 供

      System.out.println(Arrays.toString(utf8Bytes) + ":utf8Bytes");

      System.out.println();

      System.out.println(Arrays.toString(defaultBytes) + ":defaultBytes");

    } catch (UnsupportedEncodingException e) {

      e.printStackTrace();

    }


  } // main


}



展开阅读全文