Helper for getting strings from any file type in /assets/ folder. - Android App

Android examples for App:Assets String

Description

Helper for getting strings from any file type in /assets/ folder.

Demo Code


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

public class Main {
    /**//from ww  w  .ja  v  a2  s.co  m
     * Helper for getting strings from any file type in /assets/ folder. Primarily used for shaders.
     *
     * @param ctx Context to use
     * @param filename name of the file, including any folders, inside of the /assets/ folder.
     * @return String of contents of file, lines separated by <code>\n</code>
     * @throws java.io.IOException if file is not found
     */
    public static String getStringFromFileInAssets(Context ctx,
            String filename) throws IOException {
        return getStringFromFileInAssets(ctx, filename, true);
    }

    public static String getStringFromFileInAssets(Context ctx,
            String filename, boolean useNewline) throws IOException {
        InputStream is = ctx.getAssets().open(filename);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + (useNewline ? "\n" : ""));
        }
        is.close();
        return builder.toString();
    }
}

Related Tutorials