04 August, 2014

Java / Android: Raw File operation Tips

1, How to read raw file dynamically?

First, get resource id by this method:
context.getResources().getIdentifier("testcase" + case_id,
   "raw", context.getApplicationInfo().packageName);

Then open it by calling
InputStreamReader reader = new InputStreamReader(context
     .getResources().openRawResource([ID you got]));

2, How to cycle read the file?

Yes, we can set mark(), and reset the reader.
But keep in mind, not all readers can be mark-support.

So an easy way is to just close the current reader, start a new reader.
reader.close();
reader = new InputStreamReader(context.getResources().openRawResource([ID you got]));