get File Content via Context - Android android.content

Android examples for android.content:Context

Description

get File Content via Context

Demo Code

import android.content.Context;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Main{

    public static String getFileContent(Context ctx, String path) {
        FileInputStream in = null;
        try {//  w  ww .j a  v  a 2s  .c om
            in = ctx.openFileInput(path);
        } catch (FileNotFoundException e) {
            return "";
        }

        BufferedReader br;
        try {
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append('\n');
            }
        } catch (IOException e) {
        }
        return sb.toString();
    }

}

Related Tutorials