Skip to content Skip to sidebar Skip to footer

Reading A Textfile From R.raw On Android

I've got a json file in R.raw.test123, I need to process that with GSON. Step one is; read the text into a string, I want to do that using; BufferedReader r = new BufferedReader(n

Solution 1:

As answered by hooked82 you can use get the inputstream with:

InputStreamstream= getResources().openRawResource(R.raw.test123);

and then using method to convert it into string:

privatestatic String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append((line + "\n"));
        }
    } catch (IOException e) {
        Log.w("LOG", e.getMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.w("LOG", e.getMessage());
        }
    }
    return sb.toString();
}

So you can get the string with convertStreamToString(stream);.

Solution 2:

How about doing the following:

InputStreamstream= getResources().openRawResource(R.raw.test123);

Post a Comment for "Reading A Textfile From R.raw On Android"