English translation
Android File Storage: Persisting Data in Files
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In Android app development, data storage is a critically important topic. In the previous chapter, we discussed using SQLite databases for data persistence; this chapter focuses on another essential storage method—file storage. File storage is ideal for saving unstructured data such as plain text, log files, images, and other binary or textual content.
Concepts of File Storage
In Android, file storage is divided into two categories: internal storage and external storage.
- Internal Storage: Files are stored within the app’s private directory and are inaccessible to other apps—ideal for sensitive or confidential data.
- External Storage: Files reside in shared, publicly accessible locations (e.g., SD cards or shared internal storage) and can be accessed by other apps. Note that external storage may be removable or even unavailable (e.g., when mounted as a USB drive).
Core File Storage APIs
In Android, file I/O operations commonly rely on File, FileOutputStream, and FileInputStream.
Example: Internal Storage
Below is a simple example demonstrating how to create and read files in internal storage.
Writing to a File
public void writeToFile(String fileName, String data) {
FileOutputStream fos = null;
try {
// Open a private file output stream in internal storage
fos = openFileOutput(fileName, Context.MODE_PRIVATE);
// Write the string data as bytes
fos.write(data.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
In the code above, openFileOutput() obtains a FileOutputStream targeting the app’s private internal storage directory, and the string data is written as raw bytes.
Reading from a File
public String readFromFile(String fileName) {
FileInputStream fis = null;
StringBuilder stringBuilder = new StringBuilder();
try {
// Open a private file input stream in internal storage
fis = openFileInput(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
Here, openFileInput() retrieves a FileInputStream for reading from internal storage, and BufferedReader reads the file line-by-line.
Example: External Storage
When using external storage, you must first verify and request appropriate permissions.
Declaring External Storage Permissions
Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
⚠️ Note for Android 6.0 (API level 23) and higher: These permissions must also be requested at runtime.
Requesting Permissions at Runtime
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
Writing to External Storage
public void writeToExternalStorage(String fileName, String data) {
File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(externalDir, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This example writes data to the public Documents/ directory on external storage.
Reading from External Storage
public String readFromExternalStorage(String fileName) {
File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(externalDir, fileName);
StringBuilder stringBuilder = new StringBuilder();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
This reads the specified file from the public Documents/ directory using standard Java I/O classes.
Summary
Through the examples and explanations above, we’ve covered how to implement file storage in Android—including core APIs and usage patterns for both internal and external storage. File storage is especially well-suited for unstructured data and complements SQLite database usage: choose the right tool based on your data structure, access requirements, and security needs.
In the next chapter, we’ll explore Room Database, a modern, annotation-based abstraction layer over SQLite. Room simplifies database operations, enforces compile-time checks, and integrates seamlessly with Android architecture components—offering greater flexibility and developer convenience.
Stay tuned for the next chapter!
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Android File Storage: Persisting Data in Files?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue