Android File IO to already existing resource files
suppose you have file res/raw/textfile.txt
- you may have some data that you want to load with application that is static
and you store in this file
Change: to open file use Activity.getResources().openRawResource(R.raw.textfile);
EXAMPLE: reading from a static resource file res/raw/textfile.txt
App's Activity
public class FilesActivity extends Activity {
EditText textBox;
static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textBox = (EditText) findViewById(R.id.txtText1);
//STEP 1 get file to read from it. NOTE: reading from res/raw/textfile.txt
InputStream is = this.getResources().openRawResource(R.raw.textfile);
BufferedReader br = new BufferedReader(new InputStreamReader(is)); //STEP 2 Convert to BufferedReader
String str = null;
try {
while ((str = br.readLine()) != null) { //STEP 3: read one line at a time until end of file
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
is.close(); //STEP 3: close all input streams
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
