Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static final String JSON_FOLDER = "assets/json/";

    /**
     * @param context
     * @param folder   "assets/json/"
     * @param fileName
     * @return
     */
    public static final String getString(Context context, String folder, String fileName) {
        if (TextUtils.isEmpty(folder)) {
            folder = JSON_FOLDER;
        }
        StringBuffer stringBuffer = new StringBuffer();
        try {
            //            InputStream abpath = context.getClass().getResourceAsStream(folder + fileName);
            InputStream abpath = context.getClass().getClassLoader().getResourceAsStream(folder + fileName);
            if (abpath == null) {
                return "";
            }
            // BufferedReader bufferedReader = new BufferedReader(new
            // FileReader(
            // new File("file:///android_asset/json/" + fileName)));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(abpath));
            String str = "";
            while ((str = bufferedReader.readLine()) != null) {
                stringBuffer.append(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    }
}