get String Json Object From Asset - Android File Input Output

Android examples for File Input Output:Json String

Description

get String Json Object 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 getStringJsonObjectFromAsset(Context context,
            String file) {/*from w w w  . j a va2s .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