Android Context Get readRaw(Context ctx, int res_id)

Here you can find the source of readRaw(Context ctx, int res_id)

Description

Method to read in a text file placed in the res/raw directory of the application.

Declaration


public static void readRaw(Context ctx, int res_id) 

Method Source 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   w  w w .j  av a2s.  c  o m*/
     * Method to read in a text file placed in the res/raw directory of the
     * application. The method reads in all lines of the file sequentially.
     */

    public static void readRaw(Context ctx, int res_id) {

        InputStream is = ctx.getResources().openRawResource(res_id);
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
        // size

        // More efficient (less readable) implementation of above is the
        // composite expression
        /*
         * BufferedReader br = new BufferedReader(new InputStreamReader(
         * this.getResources().openRawResource(R.raw.textfile)), 8192);
         */

        try {
            String test;
            while (true) {
                test = br.readLine();
                // readLine() returns null if no more lines in the file
                if (test == null)
                    break;
            }
            isr.close();
            is.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Related

  1. getStringFromXml(Context context, int resId)
  2. getBoolean(ContentValues alarm, String key)
  3. findOne(Context context, Uri uri)
  4. getCurrentTimeString(Context aContext, long aDate)
  5. getCurrentTimeString(Context context)
  6. loadImageFromAsset(Context context, String id)
  7. hasTelephony(@Nonnull Context context)
  8. getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs)
  9. getPath(final Context context, final Uri uri)