There are some similarities between the Android file system and the disk-based file systems of other platforms. In Android we have many different ways to save our app data. Some of them are:
Using app-specific storage we can store data, which means only our app is used for storing data. It is stored in either internal storage volume or external storage. Use the internal storage to save sensitive data that other applications should not access.
Two ways to access storage:
Internal Storage: getFilesDir()
or getCacheDir()
.
External Storage: getExternalFilesDir()
or getExternalCacherDir()
.
Internal storage and external storage do not require permission.
If your app data is in internal storage then other apps cannot access it. If your app data is stored in external storage then data can be accessed by other apps.
If the app is uninstalled by any means the data files also get deleted.
This storage stores files that your application intends to share with other apps like media, documents, and other files.
There are some benefits to this, which are:
In this, we access the data with a media store API and storage access framework.
Permission is required to read the media file. These permissions are:
1.READ_EXTERNAL_STORAGE.
2.WRITE_EXTERNAL_STORAGE.
If your app is uninstalled by any means, the data will not be deleted from the external storage.
In the preference store data is stored in key-value pairs. Using preferences storage we can store our data as private or primitive data.
There are some benefits to this, which are:
Data is stored in key-value pairs.
Preferences can be accessed through the jetpack preference library.
The data from this can’t be accessed through other apps.
If the app is uninstalled by any means then the data gets deleted.
In database storage the data is stored as structured data in a private database. It uses the room persistence library.
There are some benefits to this, which are:
The database has structured data.
To access the data we have to use the room persistence library.
Other apps can’t access the data.
When our app is uninstalled then the data gets deleted too.
We can also save files in internal storage. These files are automatically set to private and they can’t be used by other apps.
To Create and Write a Private File in Internal Storage:
FileOutputStream()
method: It is called with the name of the file and the correct mode.
read()
method: It is used for reading files.
write()
method: It is used for writing in the file.
close()
method: It is used to close your file.
We can store files in external storage. External storage can be removable storage, while internal storage is non-removable. In external storage our files can be accessed by other apps. Our files do not get deleted if we uninstall the app.
Accessing the files on external storage:
getExternalFilesDir()
method: This method is used to open files in the external storage.
To save files in the external storage use the following methods: If your API version is 8 or greater then you can use the getExternalStoragePublicDirectory()
or if your API version is 7 or less then use getExternalStorageDirectory()
.
Firstly, in the AndroidManifest.xml file, we have to add permission to access the storage.
1 2
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> android: name = "android.permission.WRITE_EXTERNAL_STORAGE" / >
activitymain.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/etname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:ems="10" android:gravity="center_horizontal" android:hint="Enter name" android:inputType="textPersonName" /> <EditText android:id="@+id/etsuna" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:ems="10" android:gravity="center_horizontal" android:hint="Enter surname" android:inputType="textPersonName" /> <Button android:id="@+id/badd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:onClick="btnAddData" android:text="ADD" /> <Button android:id="@+id/bsave" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:onClick="btnSaveData" android:text="SAVE DATA TO FILE" /> <TextView android:id="@+id/tvreau" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:gravity="center_horizontal" android:text="TextView" /> </LinearLayout>
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.example.savedatausingfiles;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class MainActivity extends AppCompatActivity {
EditText etname, etsuna;
TextView tvresu;
Button badd, bsave;
ArrayList < Person > persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etname = findViewById(R.id.etname);
etsuna = findViewById(R.id.etsuna);
tvresu = findViewById(R.id.tvreau);
persons = new ArrayList < Person > ();
loadData();
}
public void btnAddData(View v) {
String name = etname.getText()
.toString();
String surname = etsuna.getText()
.toString();
Person person = new Person(name, surname);
persons.add(person);
setTextToTextView();
}
private void setTextToTextView() {
String text = "";
for (int i = 0; i < persons.size(); i++) {
text = text + persons.get(i)
.getName() + "," + persons.get(i)
.getSurname() + "\n";
}
tvresu.setText(text);
}
public void loadData() {
persons.clear();
File file = getApplicationContext()
.getFileStreamPath("Data.txt");
String lineFromfile;
if (file.exists()) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput("Data.txt")));
while ((lineFromfile = reader.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(lineFromfile, ",");
Person person = new Person(tokenizer.nextToken(), tokenizer.nextToken());
persons.add(person);
}
reader.close();
setTextToTextView();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
}
public void btnSaveData(View v) {
try {
FileOutputStream file = openFileOutput("Data.txt", MODE_PRIVATE);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(file);
for (int i = 0; i < persons.size(); i++) {
outputStreamWriter.write(persons.get(i)
.getName() + "," + persons.get(i)
.getSurname() + "\n");
}
outputStreamWriter.flush();
outputStreamWriter.close();
Toast.makeText(MainActivity.this, "Successfully saved", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
}
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.savedatausingfiles;
public class Person {
private String name;
private String surname;
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
Output