Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Saving data into internal storage location in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 297
    Comment on it

    Android provide many ways to store or save data of an application, like Shared Preferences, internal storage,external storage, SQLite database and storage on network.

    Here we will learn how to save data in the internal memory of the android device. This data is private in nature and is only accessible to the particular application only and the data is erased from the memory when the application is uninstalled.

    In order to save data in internal storage, data is written in files and then this file is stored in memory. To write data into file openFileOutput() method is used with parameters "filename" and "mode" (the mode could be private,public, etc,). Syntax is:

    string FILE_NAME="my_file";
    string data="Hello"
    FileOutputStream fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
    

    The openFileOutput() method returns a FileOutputStream instance. This instance is received in the object of FileOutputStream class. After this write() method is used to write one character at a time into the file and close() method closes the stream.

    fos.write(data.getBytes());
    fos.close();
    

    To read the data from the file, openFileInput() method is called. Pass the name of the file as a parameter in the method. The syntax is:

    FileInputStream fin = openFileInput(FILE_NAME);
    

    The openFileInput() returns a instance of the FileInputStream class. The openFileOutput() method returns a FileOutputStream instance. This instance is received in the object of FileInputStream class. After this read() method is called to read one character at time from the file and close() method is called to close the stream. The syntax is:

    {
    int c;
    String data="";
    while( (c = fin.read()) != -1){
    data = data + Character.toString((char)c);
    }
    fin.close();
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: