Java Base64 Encode and Decode a File (with Examples)

Since Java 8, the java.util.Base64 class provides convenient methods for encoding and decoding files to and from Base64 format. This tutorial explores how to perform Base64 encoding and decoding text and image files.

1. Base64 Encode and Decode a Text File

Encoding a text file to Base64 format involves the following steps:

  • Read the file content into a byte array with Files.readAllBytes() method.
  • Use the Base64.Encoder to encode the byte array into another byte array or String, as per requirements.
  • Optionally, handle any exceptions that might occur during the process.

Let us see an example. This example first encodes the file content into base64, and later decodes the base64 encoded content back to string for verification. The content of the file is: “This message will be written to a file.

File file = new File("c:/temp/test.txt");

//encoding
String encoded = Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
System.out.println(encoded);	

//decoding
byte[] decoded = Base64.getDecoder().decode(encoded);
System.out.println(new String(decoded));

The program output:

VGhpcyBtZXNzYWdlIHdpbGwgYmUgd3JpdHRlbiB0byBhIGZpbGUuDQo=
This message will be written to a file.

2. Base64 Encode and Decode an Image File

Encoding an image file to Base64 format is pretty much similar to the previous example of a text file. The difference is that we only deal with byte arrays because printing the image bytes makes no sense.

In the following example, we use Base64.getEncoder().encode() to read the image data into base64 encoded byte array. We can convert this byte array to string for storing or transferring over the network.

Later, we can use the encoded byte array or string to restore back into image, when needed.

File inImage = new File("c:/temp/test.jpg");
File outImage = new File("c:/temp/test_restored.jpg");

//encoding
byte[] encodedImageBytes = Base64.getEncoder().encode(Files.readAllBytes(inImage.toPath()));
String encodedImageDataAsString = new String(encodedImageBytes);  //Optionally, if needed

//decoding
byte[] decodedImageBytes = Base64.getDecoder().decode(encodedImageBytes);

Files.write(outImage.toPath(), decodedImageBytes);

We can verify that the image has been created in the specified path successfully.

3. Conclusion

This Java Base64 file encode and decode example discussed the methods and procedure to encode the content of a text file or bytes of a binary file and restore it to the original file. We can use and customize the code given in this tutorial based on our specific requirements.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode