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_APPEND);
The method openFileOutput() returns an instance of FileOutputStream. So you receive it in the object of
FileInputStream. After that you can call 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 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);
}
//string temp contains all the data of the file.
fin.close();
Android SQLite Database
Android provides several ways to store user and app data.
SQLite is one way of storing the application data, locally, on
the device. SQLite supports all the relational database
features.
No need to connect to database.Create database and perform
all CRUD operations.
Classes used for Database App
SQLiteOpenHelper Class
This class is used for opening, creating and upgrading the database. The
method onCreate() is the one used when the database table is created.
The method onUpgrade() is used when the structure of a database
table is changed, for example when a new field is added or the type of
a field is changed.
In order to be used, the class SQLiteOpenHelper must be extended by a
class from the app.
SQLiteDatabase Class
The SQLiteDatabase class is used to create a SQLiteDatabase object.
This object uses theDatabaseHelper object to open the writable
database. The SQLiteDatabase object can then use the standard
insert(), update(), delete(), and query() operations to manipulate the
records within the database table.
Cursor Class
To navigate on the queried database records Android provides a class
called Cursor. More details on that here. The class contains the
methods: moveToFirst(), moveToNext(),moveToPrevious() and
moveToPosition().