Sample code for 30+ languages & platforms
Java

Convert any File to Base64 (and back)

Demonstrates how to get the contents of any file as a base64 string, and then write it back.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;

public class ChilkatExample {

    public static void main(String[] args) {
        BinData bd = new BinData();

        // This example will load a PDF and return it as a base64 string.
        try {
            bd.loadFile("qa_data/pdf/helloWorld.pdf");
        } catch (ChilkatException e) {
            System.out.println("Failed to load file.");
            return;
        }

        String b64Str = bd.getEncoded("base64");
        System.out.println(b64Str);

        // Now write the base64 string back to the binary PDF file:
        BinData bd2 = new BinData();
        bd2.appendEncoded(b64Str, "base64");
        bd2.writeFile("qa_output/helloWorld.pdf");
    }
}