load Text File in UTF-8 encoding - Android File Input Output

Android examples for File Input Output:UTF File

Description

load Text File in UTF-8 encoding

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;

public class Main {
    private static final int BUFFER_SIZE = 4096;

    private static String loadTextFile(InputStream inputStream)
            throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = inputStream.read(bytes)) > 0) {
            byteStream.write(bytes, 0, len);
        }/*from  w w  w .ja  va 2s  .  c  om*/
        return new String(byteStream.toByteArray(), "UTF8");
    }

    public static final InputStream read(Context context, String path) {
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = null;

        try {
            inputStream = assetManager.open(path);
        } catch (IOException e) {
        }

        return inputStream;
    }
}

Related Tutorials