English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Implementation methods for reading file content in Android, here are several methods for your reference.
If you want to open a file stored in/data/data/<package name>/The files directory contains private files for the application, and you can use the Activity's openFileInput() method.
FileInputStream inStream = this.getContext().openFileInput("itcast.txt");
Log.i("FileTest", readInStream(inStream));
For the method readInStream(), please see the notes below this page.
Or use the absolute path of the file directly:
File file = new File("/data/data/cn.itcast.action/files/); FileInputStream inStream = new FileInputStream(file); Log.i("FileTest", readInStream(inStream));
Note:The 'cn.itcast.action' in the above file path is the package of the application. When you are writing code, you should replace it with the package used by your own application.
Private files can only be accessed by the application that creates the file. If you want the file to be readable and writable by other applications, you can specify it when creating the file
Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE permissions.
Activity also provides getCacheDir() and getFilesDir() methods:
The getCacheDir() method is used to get/data/data/<package name>/cache directory
The getFilesDir() method is used to get/data/data/<package name>/files directory
Thank you for reading, I hope it can help everyone, thank you for your support to this site!