Android Text File Read loadTextFile(Context context, String textFile)

Here you can find the source of loadTextFile(Context context, String textFile)

Description

load Text File

Declaration

public static String loadTextFile(Context context, String textFile)
            throws IOException 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;

import android.content.res.AssetManager;

public class Main {
    public static String loadTextFile(Context context, String textFile)
            throws IOException {
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(textFile);

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line = bufferedReader.readLine();
        stringBuilder.append(line);/*from  www.  j a  v  a2s  .  c om*/

        while (true) {
            if (isNullOrEmpty(line)) {
                break;
            }

            line = bufferedReader.readLine();

            if (isNullOrEmpty(line) == false) {
                stringBuilder.append("\n");
                stringBuilder.append(line);
            }
        }

        return stringBuilder.toString();
    }

    public static boolean isNullOrEmpty(String input) {
        if (input == null) {
            return true;
        }

        return input.trim().isEmpty();
    }
}

Related

  1. setFileResourceText(String path, String content)
  2. getContent(String file, String encodType)
  3. getString(File file)
  4. readFileSdcard(File file)
  5. readFromFile(Context context, String fileName)