Android Text File Read readFromFile(Context context, String fileName)

Here you can find the source of readFromFile(Context context, String fileName)

Description

read From File

Declaration

public static String readFromFile(Context context, String fileName) 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;

public class Main {
    public static String readFromFile(Context context, String fileName) {
        InputStream is = null;//from  w w w.ja v a2 s. co m
        StringBuilder sb = new StringBuilder();

        //      String temp = null;
        File file;
        if (fileName == null || fileName.equals("")) {
            return null;
        }
        try {
            file = context.getFileStreamPath(fileName);
            is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            int c;
            char[] charStr = new char[1024];
            while ((c = isr.read(charStr)) != -1) {
                sb.append(charStr, 0, c);
            }

            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return sb.toString();

    }
}

Related

  1. readTextFile(String path)
  2. setFileResourceText(String path, String content)
  3. getContent(String file, String encodType)
  4. getString(File file)
  5. readFileSdcard(File file)
  6. loadTextFile(Context context, String textFile)