read File from Asset by charset - Android App

Android examples for App:Assets File

Description

read File from Asset by charset

Demo Code


//package com.java2s;
import android.app.Activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String readFile(Activity activity, String filename,
            String charset) throws IOException {
        //"UTF-8"
        InputStream is = activity.getResources().getAssets().open(filename);
        InputStreamReader isr = new InputStreamReader(is, charset);
        BufferedReader br = new BufferedReader(isr);
        String line;//from   ww  w .  jav a  2  s  . co  m
        StringBuffer builder = new StringBuffer();
        while ((line = br.readLine()) != null) {
            builder.append(line);
        }
        br.close();
        isr.close();
        is.close();
        return builder.toString();
    }
}

Related Tutorials