Articles → Android → Interaction With Internal Storage In Android
Interaction With Internal Storage In Android
- Create a file in internal storage.
- Write in the internal storage file.
- Read data from internal storage file.
- Delete the internal storage file.
Example
- An edit text control
- A button ‘Write’ for writing data from edit text to internal storage.
- A button ‘Clear’ to clear data from edit text.
- A button ‘Read’ for reading data from internal storage and display it in edit text.
- 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();
}
}
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge
How Can We See The File In Internal Storage?
- Go to View → Tool windows → Device File Explorer
- Go to package name → files
Click to Enlarge