Articles → Android → Interaction With Internal Storage In Android

Interaction With Internal Storage In Android





  1. Create a file in internal storage.
  2. Write in the internal storage file.
  3. Read data from internal storage file.
  4. Delete the internal storage file.

Example




  1. An edit text control
  2. A button ‘Write’ for writing data from edit text to internal storage.
  3. A button ‘Clear’ to clear data from edit text.
  4. A button ‘Read’ for reading data from internal storage and display it in edit text.
  5. A button ‘Delete’ for deleting the file in internal storage.


public void WriteInInternalStorage(View view) {
	EditText name = (EditText) findViewById(R.id.name);
	String data = name.getText().toString();

	try {
		FileOutputStream outputFileStream = openFileOutput("file.txt", Context.MODE_PRIVATE);
		outputFileStream.write(data.getBytes());
		outputFileStream.close();
	} catch(Exception e) {
		e.printStackTrace();
	}
}

public void ClearData(View view) {
	EditText name = (EditText) findViewById(R.id.name);
	name.setText("");
}

public void DeleteInternalStorageFile(View view) {
	this.deleteFile("file.txt");
}

public void ReadFromInternalStorage(View view) {
	EditText name = (EditText) findViewById(R.id.name);
	try {
		InputStream inputStream = this.openFileInput("file.txt");
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

		String line = new String();
		String allLines = new String();

		while ((line = bufferedReader.readLine()) != null) {
			allLines += line;
		}

		name.setText(allLines);
		inputStream.close();
	} catch(Exception e) {
		e.printStackTrace();
	}
}






Picture showing the screen on load
Click to Enlarge



Picture showing the Write button for writing data in the internal storage in android
Click to Enlarge



Picture showing the Clear button for clearing data from the textbox
Click to Enlarge



Picture showing the Read button for reading data from the internal storage in android
Click to Enlarge


How Can We See The File In Internal Storage?




  1. Go to View → Tool windows → Device File Explorer
  2. Go to package name → files
  3. Picture showing the file in the internal storage in android
    Click to Enlarge



Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, December 25, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250