Java UTF8 stringToUtf8(String str)

Here you can find the source of stringToUtf8(String str)

Description

Encodes the specified string as a UTF-8 sequence.

License

Open Source License

Parameter

Parameter Description
str the string that will be encoded.

Exception

Parameter Description
UnsupportedCharsetException if the Java implementation does not support the UTF-8 character encoding, which is requiredof all Java implementations.

Return

an array containing the UTF-8 sequence that results from encoding the input string.

Declaration


public static byte[] stringToUtf8(String str) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
    public static final String ENCODING_NAME_UTF8 = "UTF-8";

    /**//from  w  w w. j a va2 s  .com
     * Encodes the specified string as a UTF-8 sequence.
     *
     * @param  str  the string that will be encoded.
     * @return an array containing the UTF-8 sequence that results from encoding the input string.
     * @throws UnsupportedCharsetException
     *           if the Java implementation does not support the UTF-8 character encoding, which is required
     *           of all Java implementations.
     */

    public static byte[] stringToUtf8(String str) {
        try {
            return str.getBytes(ENCODING_NAME_UTF8);
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedCharsetException(ENCODING_NAME_UTF8);
        }
    }
}

Related

  1. newUTF8Decoder()
  2. openFileUTF(String nom)
  3. putUTF8(byte[] data, int offset, String str)
  4. sort(String inFile, int keyIndex, String outFile)
  5. stringFromBytesUTF8(byte[] bytes)
  6. stringToUTF8Bytes(String str)
  7. toBytesUTF8(String s)
  8. toBytesUTF8(String str)
  9. unZip(String zipFile, String outputFolder, boolean skipDirectory)