How to read and write Base64 file

In the other tutorial, you learned how to encrypt a String and

decode it again. Now we want to write an encoded text with Base64 tool.

This is similar like in the other Tutorial. Before you start encoding it, you have to consider creating a “UTF-8( Universal Coded Character Set )” based text. Add the following line before writing a text to a specific path:

String input = new String(stream,"UTF-8");

android.util.Base64.decode(byte data,Base64.DEFAULT) – This  method decodes a string using  RFC 4648 and RFC 2045 scheme.

 public void writeFile(File ft){
        try {

            FileWriter ftw = new FileWriter(ft, true);
            BufferedWriter bw = new BufferedWriter(ftw);

            String sum = "Hello World";
            byte[] stream = android.util.Base64.encode(sum.getBytes(), Base64.DEFAULT);
            String input = new String(stream,"UTF-8");


        } catch (IOException ex) {

    public String readFile(File ft){
        String name = "";
        BufferedReader br = null;
        String response = null;
       String decodedBase64 = null;
        try {
            FileInputStream fileInputStreamReader = new FileInputStream(ft);
            byte[] bytes = new byte[(int)ft.length()];
            decodedBase64 = new String(android.util.Base64.decode(bytes,Base64.DEFAULT));
        } catch (FileNotFoundException e) {
        } catch (IOException e) {

        return decodedBase64;


Leave a Reply

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