read file from Context - Android android.content

Android examples for android.content:Context

Description

read file from Context

Demo Code

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import java.io.*;

public class Main{

    public static String read(Context context, String fileName)
            throws IOException {
        FileInputStream fileInputStream = context.openFileInput(fileName);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }/*  ww  w  .j a v a 2 s  . c  o m*/
        byte[] data = outputStream.toByteArray();
        return new String(data);
    }

}

Related Tutorials