Android read text raw resource file Android read text raw resource file android android

Android read text raw resource file


You can use this:

    try {        Resources res = getResources();        InputStream in_s = res.openRawResource(R.raw.help);        byte[] b = new byte[in_s.available()];        in_s.read(b);        txtHelp.setText(new String(b));    } catch (Exception e) {        // e.printStackTrace();        txtHelp.setText("Error: can't show help.");    }


What if you use a character-based BufferedReader instead of byte-based InputStream?

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String line = reader.readLine();while (line != null) {     ...    line = reader.readLine();}

Don't forget that readLine() skips the new-lines!


If you use IOUtils from apache "commons-io" it's even easier:

InputStream is = getResources().openRawResource(R.raw.yourNewTextFile);String s = IOUtils.toString(is);IOUtils.closeQuietly(is); // don't forget to close your streams

Dependencies: http://mvnrepository.com/artifact/commons-io/commons-io

Maven:

<dependency>    <groupId>commons-io</groupId>    <artifactId>commons-io</artifactId>    <version>2.4</version></dependency>

Gradle:

'commons-io:commons-io:2.4'