简体   繁体   中英

Android studio: Where should I put a binary file to read with object RandomAccessFile?

I'd like to open a binary file containing in a form of array. To access randomly, I used RamdomAccessFile. I put the file in the same folder as the java class file is in. I wrote the code as follows:

File file = new File("table.bin");
RandomAccessFile raf = new RandomAccessFile(file, "r");

But console says 'no such file or directory'. Then I moved to assets folder and got the same message. It worked well when I used this code in regular(not Android Studio) java file, with 'table.bin' in the same folder.

Where in the project folder do I have to place the file in?

Where in the project folder do I have to place the file in?

If you are looking to ship this file with your app, put it in assets/ . However, bear in mind that this is a file only on your development machine. It is not a file on the device. On the device, it is merely an entry in your APK file. You can get an InputStream on this via AssetManager .

If you absolutely need this to be a file — you need RandomAccessFile , you want to modify the contents, etc. — use the InputStream to make a copy of the data to a file on the local filesystem, such as on internal storage . Then, you can work with that file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM