Created
July 13, 2017 06:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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