Encodes a String to Base64 - Java java.lang

Java examples for java.lang:String Base64

Description

Encodes a String to Base64

Demo Code


//package com.java2s;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] argv) throws Exception {
        String stringToEncode = "java2s.com";
        System.out.println(encode(stringToEncode));
    }//from  w w w.ja  va  2  s .c o m

    /**
     * Encodes a String to Base64
     * @param stringToEncode The String to be encoded
     * @return The Base64 encoded String
     */
    public static String encode(String stringToEncode) {
        return java.util.Base64.getEncoder().encodeToString(
                stringToEncode.getBytes(StandardCharsets.UTF_8));
    }
}

Related Tutorials