read Resource using InputStreamReader - Android java.io

Android examples for java.io:InputStreamReader

Description

read Resource using InputStreamReader

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import android.content.Context;

public class Main {

  public static String readResource(Context c, int id) throws IOException {
    InputStream is = c.getResources().openRawResource(id);
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {/*from www .  j ava  2  s  .  c o  m*/
      Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      int n;
      while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
      }
    } finally {
      is.close();
    }
    return writer.toString();
  }

}

Related Tutorials