read Text From Asset File using InputStreamReader - Android App

Android examples for App:Assets File

Description

read Text From Asset File using InputStreamReader

Demo 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 {
    private static final String ASSET_FOLDER_PREFIX = "file:///android_asset/";

    public static String readTextFromFile(Context context, String filePath)
            throws IOException {
        StringBuilder builder = new StringBuilder();

        AssetManager manager = context.getAssets();
        InputStream stream = manager.open(filePath.replace(
                ASSET_FOLDER_PREFIX, ""));
        InputStreamReader streamReader = new InputStreamReader(stream);
        BufferedReader bufferedReader = new BufferedReader(streamReader);
        String line;/*from   w  w  w.  j  a  v a  2s . c o  m*/
        while ((line = bufferedReader.readLine()) != null) {
            builder.append(line);
            builder.append('\n');
        }
        bufferedReader.close();

        return builder.toString();
    }
}

Related Tutorials