Sunday 12 January 2020

How to get UTF-8 String literal constant?


'StandardCharsets' class provides all the standard character encodings supported by Java. As per java specification, charsets provided by this class are guaranteed to be available on every implementation of the Java
 platform.

Example
String utf8 = StandardCharsets.UTF_8.name();

App.java

 package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;

public class App {

 public static void main(String args[]) throws MalformedURLException, UnsupportedEncodingException {

  String iso8859_1 = StandardCharsets.ISO_8859_1.name();
  String usAscii = StandardCharsets.US_ASCII.name();
  String utf16 = StandardCharsets.UTF_16.name();
  String utf16be = StandardCharsets.UTF_16BE.name();
  String utf16le = StandardCharsets.UTF_16LE.name();
  String utf8 = StandardCharsets.UTF_8.name();

  System.out.println(iso8859_1);
  System.out.println(usAscii);
  System.out.println(utf16);
  System.out.println(utf16be);
  System.out.println(utf16le);
  System.out.println(utf8);
 }

}


Output
ISO-8859-1
US-ASCII
UTF-16
UTF-16BE
UTF-16LE
UTF-8

You may like

No comments:

Post a Comment