Java base64 encode and decode example

Java base64 encode and decode example.

String original = "hello world";

// encode
byte[] encodedBytes = Base64.getEncoder().encode(original.getBytes());
String encoded = new String(encodedBytes);
System.out.println(encoded); // output aGVsbG8gd29ybGQ=

byte[] decodedBytes = Base64.getDecoder().decode(encoded.getBytes());
String decoded = new String(decodedBytes);
System.out.println(decoded); // output original: hello world

java