41

I have a text file i want to include in my Android application, it is not a string file it is a standard text file. It contains data that defines the characteristics of a "map" that is drawn on a board. The file is not an XML file so i am unsure where i should put it or if this isn't good file structure for android? Are you suppose to do this? If you are then under what directory are you suppose to put them? How then are you suppose to access the file? I know how to use FileInputStreams and FileOutputStreams i just need to know how to access the file. All relevant answers are welcome and appreciated!

John
  • 3,769
  • 6
  • 30
  • 49
  • Seems like someone already found the answer in another question [here][1]. [1]: http://stackoverflow.com/questions/4087674/android-read-txt-raw-resource-file – onit Feb 02 '12 at 21:19

3 Answers3

45

Use assets or raw folder in your android folders structure to keep that file. For more info read this

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    accepted answer here has more info regarding assets folder: http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio – Colin Sep 30 '15 at 17:51
  • How can I read file from app folder in android app? If I keep .properties file under assets folder then it's visible through reverse engineering. – Satish Sojitra May 03 '17 at 07:00
  • 1
    Never bake your secret stuff within the app/source-code... always request from an online source where you'll have more control. This is the rule of thumb. – waqaslam May 03 '17 at 07:52
  • You cannot write into your file if your file is in assets. – Talha Ghous Oct 10 '17 at 01:33
22

You have to put your file in the assets folder as Waqas said.

Now to access it you do it like that.

I give you an example using BufferedReader

BufferedReader reader = new BufferedReader(
                 new InputStreamReader(getAssets().open("YourTextFile.txt")));

Be careful. In my case, I don't know why for now, I cannot read text files bigger than ~1MB and I had to split them in multiple small files. It seems other had the same problem of file size but I didn't find any information about that on Android developer site. If any one knows more about this ....

FOLLOW UP

My problem with the 1MB was due to a know bug/limitation of earlier versions of Android. Since using recent versions of Android, that problem is not present anymore.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • I just though Waqas was missing showing an example as you asked "i just need to know how to access the file" and so for me the answer was "incomplete" and also to give you the warning about the text file size. But it's not a competition ! We are here to help each others – HpTerm Feb 02 '12 at 21:38
  • 3
    You can actually access the assets without opening a stream and reading the file byte by byte. You can instead use `file://android_asset/file.txt` which gives you access to the file right away. You can create a File object out of it or do whatever you like. – asenovm Feb 02 '12 at 22:14
0

I would just like to add to the accepted answer (I don't have enough reputation to comment unfortunately.) The link there to the tutorial that explains how to set up the res/raw method or the assets method is mostly good, but there's actually a MUCH easier way. Look at the function described there called LoadFile. That function is rather verbose. Lets say all you need is an InputStream variable so that you can read and write to a file. Then delete everything after line 77! Also you don't need the resource id at all! You can use this function:

//get the file as a stream
iS = resources.openRawResource(R.raw.mygpslog)

Now all you have to do is return iS and you will have your much desired file handle.

user3308807
  • 25
  • 1
  • 8