Android file data storage
1. Introduction to file saving data
Activity provides the openFileOutput() method that can be used to output data to a file. The specific implementation process is the same as saving data to a file in a J2SE environment. Files can be used to store large amounts of data, such as text, books, audio, etc.
File objects are suitable for reading or writing large amounts of data without skipping from start to end. For example, it is suitable for image files or anything exchanged over the network.
The default location of data storage: /data/data/<package name>/files/***.***.***.
All Android devices have two file storage areas: "internal" and "external" storage. This article mainly stores data, so stores files in the "internal" storage area.
2. How to use
1. Write content to the file
try { FileOutputStream fos = (mFileName,Context.MODE_PRIVATE); (()); (); }catch (Exception e){ (); }
The first parameter of the openFileOutput() method is used to specify the file name and cannot contain the path splitter "/". If the file does not exist, Android will automatically create it. The second parameter of the openFileOutput() method is used to specify the operation mode.
The operating modes are:
Context.MODE_PRIVATE = 0: It is a silent operation mode, which means that private data is used when changing the file and can only be accessed by the application itself. In the modification mode, the written content will overwrite the content of the original file. If you want to append the newly written content to the original file, you can use Context.MODE_APPEND.
Context.MODE_APPEND = 32768: The mode will check whether the file exists. If it exists, it will be added to the file, otherwise a new file will be created.
Context.MODE_WORLD_READABLE and Context.MODE_WORLD_WRITEABLE are used to control whether other applications have permission to read and write files.
Context.MODE_WORLD_READABLE = 1: Indicates that the current file can be read by other applications.
Context.MODE_WORLD_WRITEABLE = 2: Indicates that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass in Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE. Android has its own security model. When the application (.apk) is installed, the system will assign it a userid. When it is time to access other resources such as files, the userid needs to be matched. By default, any application created files, SharedPrefrences, and databases should be private and cannot be accessed by other programs. Unless Context.MODE_WORLD_READABLE or Context.MODE_WORLD_WRITEABLE is formulated during creation, only in this way other programs can access them normally.
2. Read the file contents
try { FileInputStream fis = (mFileName); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); String info = (); (); return info; }catch (Exception e){ (); }
3. Small cases
1. Add a file
<string name="write_data">Write data</string> <string name="read_data">Read data</string> <string name="file">File</string>
2. Modify the activity_main.xml file
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/file" android:layout_gravity="center_horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="@dimen/fab_margin" android:layout_marginBottom="@dimen/fab_margin" > <Button android: android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="@string/write_data" /> <Button android: android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="@string/read_data" /> </LinearLayout>
3. Add FileDBManager class
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by zhangmiao on 2016/12/20. */ public class FileDBManager { private File mFile; private Context mContext; private String mFileName = "myfile"; public FileDBManager(Context context){ mContext = context; } public void write(String info){ try { FileOutputStream fos = (mFileName,Context.MODE_PRIVATE); (()); (); }catch (Exception e){ (); } } public String read(){ try { FileInputStream fis = (mFileName); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); String info = (); (); return info; }catch (Exception e){ (); } return ""; } }
4. Modify MainActivity
package ; import ; import ; import ; import ; import ; import .; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity implements {private FileDBManager mFileManager;private TextView mTableInfo; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mFileManager = new FileDBManager(this); mTableInfo = (TextView) findViewById(.table_info); (this); (this); } @Override public void onClick(View v) { switch (()) { case .file_write: ("hello world!"); break; case .file_read: (()); break;default:break; } } }
Thank you for reading, I hope it can help you. Thank you for your support for this site!