SoFunction
Updated on 2025-04-12

Summary of the implementation method of reading file content on Android

How to implement Android to read file contents? Here are several methods, you can take a look at it if you need it.

If you want to open a file stored in the /data/data/<package name>/files directory application privately, you can use Activity to provide the openFileInput() method.

FileInputStream inStream = ().openFileInput("");
("FileTest", readInStream(inStream));

Please refer to the note below this page for the readInStream() method.

Or use the absolute path to the file directly:

File file = new File("/data/data//files/");
FileInputStream inStream = new FileInputStream(file);
("FileTest", readInStream(inStream));

Notice:The "" in the file path above is the package where the application is located. When you write the code, you should replace it with the package that you use for your application.

For private files, they can only be accessed by applications that create the file. If you want the file to be read and written by other applications, you can specify when creating the file

Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE permissions.

Activity also provides getCacheDir() and getFilesDir() methods:

getCacheDir() method is used to get /data/data/<package name>/cache directory
getFilesDir() method is used to get /data/data/<package name>/files directory

Thank you for reading, I hope it can help you. Thank you for your support for this site!