get Asset String via BufferedReader - Android App

Android examples for App:Assets String

Description

get Asset String via BufferedReader

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;

public class Main {

    public static String getAssetString(String asset, Context context) {
        BufferedReader bufferedReader = null;
        try {/*from  w w w  .j  av  a  2 s. c o m*/
            bufferedReader = new BufferedReader(new InputStreamReader(
                    context.getAssets().open(asset)));
            String line = null;
            StringBuilder builder = new StringBuilder();
            while (null != (line = bufferedReader.readLine())) {
                builder.append(line).append("\n");
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            bufferedReader = null;
        }
        return "";
    }
}

Related Tutorials