Skip to content

Instantly share code, notes, and snippets.

@francisnnumbi
Created July 13, 2017 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisnnumbi/4540ae636bd4801e3a8bf134ff4ac98d to your computer and use it in GitHub Desktop.
Save francisnnumbi/4540ae636bd4801e3a8bf134ff4ac98d to your computer and use it in GitHub Desktop.
Simple class that reads res/raw text file using Scanner.class It has got one static method that require the Context and the resource id of the file to be read. It is a simple demonstration of reusability. The other way is to use a succession of InputStream - InputStreamReader - BufferedReader to read that res/raw text file.
import android.content.res.*;
import java.util.*;
import android.content.*;
public class InnerReader
{
public static String readText(Context context, int resId){
StringBuilder sb = new StringBuilder();
Scanner reader = new Scanner(context.getResources().openRawResource(resId));
try{
while(reader.hasNextLine()){
sb.append(reader.nextLine());
sb.append(System.lineSeparator());
}
}catch(Exception e){}
finally{
reader.close();
}
return sb.toString().trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment