FileIO with Android
Reading and writing files in Android are not different from reading and writing files in standard Java. Same
java.io package can be used. However, there is some specific related to the folders where you are allowed to write, permissions in general and MTP work arounds.
Android provides means for sharing the file between multiple applications as documented here. This is not required if there is only one app that creates and uses the file.
Android provides alternative storage options like shared and private preferences, saved bundles and built-in database. In some cases, they are better choice than just using plain files.
Android activity does have few specific methods that look like replacements of the Java standard File IO methods. For instance, instead for
File.delete() you can call
Context.deleteFile(), and instead of applying
File.listFiles() recursively you can call
Context.fileList() to get the list of all your app specific files with somewhat less code. However, they do not provide extra functionality beyond standard
Obtaining the working folder
You can get your working folder by calling the method
getFilesDir() on your Activity (Activity is the central class in your application that inherits from Context. See here). Reading is not different. Only your application will have access to this folder.
Your activity could contain the following code, for instance:
Serializing the object
The old good Java object serialization is available for you in Android. you can define Serializable classes like:
and then write then to the ObjectOutputStream:
Java object serialization may be either perfect or really bad choice, depending on what do you want to do with it - outside the scope of this tutorial and sometimes opinion based. Read about the versioning first if you decide to use it.
Solving "Invisible MTP files" problem.
If you create files for exporting via USB cable to desktop using MTP protocol, may be a problem that newly created files are not immediately visible in the file explorer running on the connected desktop PC. To to make new files visible, you need to call MediaScannerConnection:
This MediaScannerConnection call code works for files only, not for directories. The problem is described in this Android bug report. This may be fixed for some version in the future, or on some devices.
Working with big files
Small files are processed in a fraction of second and you can read / write them in place of the code where you need this. However if the file is bigger or otherwise slower to process, you may need to use AsyncTask in Android to work with the file in the background:
Writing raw array of bytes
There is nothing Android specific with this code. If you write lots of small values often, use BufferedOutputStream to reduce the wear of the device internal SSD.
Writing to external storage (SD card)
You can also read and write from/to memory card (SD card) that is present in many Android devices. Files in this location can be accessed by other programs, also directly by the user after connecting device to PC via USB cable and enabling MTP protocol.
Finding the SD card location is somewhat more problematic. The Environment class contains static methods to get "external directories" that should normally be inside the SD card, also information if the SD card exists at all and is writable. This question contains valuable answers how to make sure the right location will be found.
Accessing external storage requires permissions in you Android manifest:
For older versions of Android putting permissions it is enough to put these permissions into manifest (the user must approve during installation). However starting from Android 6.0 Android asks the user for approval at the time of the first access, and you must support this new approach. Otherwise access is denied regardless of your manifest.
In Android 6.0, first you need to check for permission, then, if not granted, request it. The code examples can be found inside this SO question.