Depending on what you are going to do with this file. If your goal is to only read the file, then the resource folder is the way to go. If you want to save information in this file when you work with it, you must place it on the device.
If you choose option number 2, you need to decide if you want other applications to read the file. More information can be found at this address:
http://developer.android.com/training/basics/data-storage/files.html
In addition, you can read / write directly to the device with the standard java procedure, as described above. Although, the file path is likely to be
"/SDCard/text.txt"
EDIT:
Here is a code snippet to get started with
FileInputStream is; BufferedReader reader; final File file = new File("/sdcard/text.txt"); if (file.exists()) { is = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(is)); String line = reader.readLine(); while(line != null){ Log.d("StackOverflow", line); line = reader.readLine(); } }
But it assumes that you know that you put text.txt at the root of your SD card.
If the file is located in the resource folder, you should do this:
BufferedReader reader; try{ final InputStream file = getAssets().open("text.txt"); reader = new BufferedReader(new InputStreamReader(file)); String line = reader.readLine(); while(line != null){ Log.d("StackOverflow", line); line = reader.readLine(); } } catch(IOException ioe){ ioe.printStackTrace(); }
source share