read Html For asset file Name - Android App

Android examples for App:Assets File

Description

read Html For asset file Name

Demo Code


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

public class Main {
    public static final String NULL = new String();
    public static final String HTML_DIR = "html";
    public static final String HTML_SUFFIX = ".html";

    public static String readHtmlForName(Context context, String fileName) {
        if (context == null || isNull(fileName))
            return NULL;

        try {//from ww  w.  j a  v  a  2 s .  c o m
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    context.getAssets().open(
                            HTML_DIR + File.separator + fileName
                                    + HTML_SUFFIX)));

            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return NULL;
    }

    private static boolean isNull(String str) {
        return str == null || "".equals(str);
    }
}

Related Tutorials