Internal Storage

  • If we want to store the data in the file then we can save these files in the internal storage of the device.
  • When we save files into the internal storage then it is called as private means other applications cannot access these files.
  • When we remove the application then these files also remove from the internal storage.
  • If we want to store the file in internal storage then we have to call the openFileOutput() method. This method returns the object of the FileOutputStream class.
  • This class is used to represent the data through the file.
  • When we call the openFileOutput() method then we have to pass the name of file and mode of file.
  • There are four types of mode as follows:
    • MODE_PRIVATE : this is default mode and in this mode files remain private.
    • MODE_APPEND : if the file is exist that is passed by us as parameter then system will not create new file; use the existing file to add the data.
    • MODE_WORLD_READABLE: when we create these types of files then it increases the risk because any application can read or access this file.
    • MODE_WORLD_WRITABLE: this kind of file can be altered or modified by any other application.
  • After defining the mode, we have to call the write() method using the object of FileOurputStream class, save the file and close the output stream using the close() method.
  • The demo example is below:
  • String name="Name";
    FileOutputStream obj = openFileOutput(fileName,Context.MODE_PRIVATE);
    obj.write(name.getBytes()); // write data in form of byte only.
    obj.close(); // closing stream
  • To read the data from files we have to use the openFileInput() method. This method returns the object of FileInputStream class; and inside this method we have to pass the name of file as a parameter. After that we have to call the read() method using FileInputStream class object as follows:
  • FileInputStream obj = openFileInput(file_Name);;