Android provides many kinds of storage for applications to store their data. These storage places are shared preferences, internal and external storage, SQLite storage, and storage via network connection. Internal storage is the storage of the private data on the device memory. By default these files are private and are accessed by only your application and get deleted , when the user deletes your application.
Writing file
In order to use internal storage to write some data in the file, call the openFileOutput() method with the name of the file and the mode. The mode could be private , public e.t.c. Its syntax is given below −
FileOutputStream fOut = openFileOutput(“file name here”,MODE_WORLD_READABLE);
The method openFileOutput() returns an instance of FileOutputStream. So you receive it in the object of FileInputStream. After that you can call the write method to write data on the file. Its syntax is given below −
String str = “data”;
fOut.write(str.getBytes());
fOut.close();
Reading file
In order to read from the file you just created , call the openFileInput() method with the name of the file. It returns an instance of FileInputStream. Its syntax is given below −
FileInputStream fin = openFileInput(file);
After that, you can call the read method to read one character at a time from the file and then you can print it. Its syntax is given below −
int c;
String temp=””;
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
Apart from the methods of write and close, there are other methods provided by the FileOutputStream class for better writing files. These methods are listed below −
Sr.No | Method & description |
1 | FileOutputStream(File file, boolean append)This method constructs a new FileOutputStream that writes to a file. |
2 | getChannel()This method returns a write-only FileChannel that shares its position with this stream |
3 | getFD()This method returns the underlying file descriptor |
4 | write(byte[] buffer, int byteOffset, int byteCount)This method Writes count bytes from the byte array buffer starting at position offset to this stream |
Apart from the methods of read and close, there are other methods provided by the FileInputStream class for better reading files. These methods are listed below −
Sr.No | Method & description |
1 | available()This method returns an estimated number of bytes that can be read or skipped without blocking for more input |
2 | getChannel()This method returns a read-only FileChannel that shares its position with this stream |
3 | getFD()This method returns the underlying file descriptor |
4 | read(byte[] buffer, int byteOffset, int byteCount)This method reads at most length bytes from this stream and stores them in the byte array b starting at offset |
SQLite Databases
SQLite is an open source SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don’t need to establish any kind of connections for it like JDBC,ODBC e.t.c
Database – Package
The main package is android.database.sqlite that contains the classes to manage your own databases
Database – Creation
In order to create a database you just need to call this method openOrCreateDatabase with your database name and mode as a parameter. It returns an instance of SQLite database which you have to receive in your own object.Its syntax is given below
SQLiteDatabase mydatabase = openOrCreateDatabase(“db name”,MODE_PRIVATE,null);
Sr.No | Method & Description |
1 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler)This method only opens the existing database with the appropriate flag mode. The common flags mode could be OPEN_READWRITE OPEN_READONLY |
2 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)It is similar to the above method as it also opens the existing database but it does not define any handler to handle the errors of databases |
3 | openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)It not only opens but creates the database if it does not exist. This method is equivalent to the openDatabase method. |
4 | openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)This method is similar to the above method but it takes the File object as a path rather than a string. It is equivalent to file.getPath() |