As part of a project, i had to read a resource file from raw folder. This was a login response json file.
First right clicked on the Resources folder and selected the resource directory like in the below screenshot
From this, selected the resource type as raw.
First right clicked on the Resources folder and selected the resource directory like in the below screenshot
From this, selected the resource type as raw.
Doing this, the raw folder appeared under resources.
Right clicked on the raw again and then selected add file and selected a text file as the file was a json file
This created an empty file and pasted the json file contents into this. Did a compile once so that R.java file is getting this resource Id
After this from the code did the below
Utils.readRawTextFile(contextRef,R.raw.login_response);
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
Thats all, the file is read from the resouces folder.
No comments:
Post a Comment