Java Base64

Share

Java Base64

In Java, you can encode and decode data in Base64 format using classes provided in the java.util.Base64 package, which was introduced in Java 8. Base64 encoding is commonly used for encoding binary data, such as images or files, into a text-based format that can be safely transmitted as text or used in data storage.

Here are examples of how to perform Base64 encoding and decoding in Java:

Base64 Encoding:

To encode binary data into Base64 format, you can use the Base64.getEncoder() class from the java.util.Base64 package:

java

import java.util.Base64;

public class Base64EncodingExample {
public static void main(String[] args) {
// Binary data to be encoded
byte[] binaryData = “Hello, Base64!”.getBytes();

// Encode binary data to Base64
byte[] encodedData = Base64.getEncoder().encode(binaryData);

// Convert the encoded data to a string
String base64String = new String(encodedData);

System.out.println("Base64 Encoded String: " + base64String);
}
}

Base64 Decoding:

To decode a Base64-encoded string back to its original binary form, you can use the Base64.getDecoder() class:

java

import java.util.Base64;

public class Base64DecodingExample {
public static void main(String[] args) {
// Base64-encoded string
String base64String = “SGVsbG8sIEJhc2U2NCEh”;

// Decode the Base64-encoded string to binary data
byte[] decodedData = Base64.getDecoder().decode(base64String);

// Convert the decoded data to a string
String originalString = new String(decodedData);

System.out.println("Decoded String: " + originalString);
}
}

These examples demonstrate how to use the Base64.getEncoder() and Base64.getDecoder() methods to perform Base64 encoding and decoding in Java. You can use these methods to encode and decode data for various purposes, such as data transmission, storing binary data in a text-based format, or working with web APIs that require Base64 encoding.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *