English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Methods for reading and writing private files in Android programming

This article describes the method for reading and writing private files in Android programming. Share it with everyone for reference, as follows:

A private file refers to a file that the program itself can read, while other programs do not have permission to access. This file is stored in the Data.app.package.file directory below.

The method for writing files is relatively simple:

private void writeFile(String fileName, String info) {
 try {
  FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
  byte[] bytes = info.getBytes();
  fout.write(bytes);
  fout.close();
 }
 }
}

This can complete the writing to a private file, and when writing to a private file, the file used is openFileOutput.

The above code performs writing to a private file, and the following code is for reading from the private file:

private String readFile(String fileName) {
 try {
  FileInputStream fin = openFileInput(fileName);
  int length = fin.available();// Get file length
  byte[] bytes = new byte[length];
  fin.read(bytes);
  return EncodingUtils.getString(bytes, ENCODING);
 }
  return "";
 }
}

Use 'openFileInput' to read private files!

Readers who are interested in more about Android-related content can check the special topics on this site: 'Summary of Android File Operation Skills', 'Summary of Android Resource Operation Skills', 'Summary of Android XML Data Operation Skills', 'Summary of Activity Operation Skills in Android Programming', 'Summary of SQLite Database Operation Skills in Android', 'Summary of JSON Data Operation Skills in Android', 'Summary of Database Operation Skills in Android', 'Summary of SD Card Operation Methods in Android Programming', 'Tutorial on Android Development for Beginners and Advanced Users', 'Summary of View View Skills in Android', and 'Summary of Android Control Usage'

I hope the description in this article will be helpful to everyone in Android program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please report any infringement by email to codebox.com (replace # with @), and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like