Sample code for 30+ languages & platforms
Java

Get Contents of File as Base64

See more Encryption Examples

Demonstrates how to read the contents of a file and convert to a base64 string.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkBinData bd = new CkBinData();

    success = bd.LoadFile("qa_data/jpg/starfish.jpg");
    if (success == false) {
        System.out.println("Failed to load file.");
        return;
        }

    System.out.println(bd.getEncoded("base64"));

    // If you want mult-line base64:
    System.out.println("--");
    System.out.println(bd.getEncoded("base64_mime"));

    // If you want hex..
    System.out.println("--");
    System.out.println(bd.getEncoded("hex"));

    // etc.
  }
}