Java Base64 Encode Example

In this source code example, we show you how to encode (Base64) any arbitrary text in Java using the Base64 API that was introduced in Java 8.

Base64 is a binary-to-text encoding scheme that represents binary data in a printable ASCII string format.

Java Base64 Encode Example

import java.nio.charset.StandardCharsets;
import java.util.Base64;

class Base64EncodeExample {

    private static String base64Encode(String value) {
        try {
            return Base64.getEncoder()
        } catch(UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);

    public static void main(String[] args) {
        String data = "sourcecode:examples!?$*&()'-=@~";

        String encodedData = base64Encode(data);

        System.out.println("encodedData -> " + encodedData);
encodedData -> c291cmNlY29kZTpleGFtcGxlcyE/JComKCknLT1Afg==