get String Json Array From Asset - Android File Input Output

Android examples for File Input Output:Json String

Description

get String Json Array From Asset

Demo Code


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

public class Main {
    public static String getStringJsonArrayFromAsset(Context context,
            String file) {//from   w  w w.  j ava  2 s. c  om
        String filePath = String.format("json/%s.json", file);
        AssetManager assetManager = context.getAssets();
        try {
            InputStream is = assetManager.open(filePath);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            return new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related Tutorials