Android-based file data storage and android-based Data Storage
1. Introduction to file storage data
Activity provides the openFileOutput () method to output data to a file. The specific implementation process is the same as saving data to a file in the J2SE environment. Files can be used to store a large amount of data, such as text, books, and audio.
The File object is suitable for reading or writing large amounts of data without skipping from the start to the end. For example, it is suitable for image files or any content exchanged over the network.
Default data storage location:/data/<package name>/files /***.***.
All Android devices have two file storage areas: "internal" and "external. This article mainly stores data, so files are stored in the "internal" storage area.
Ii. usage method 1. Write content to the file
try { FileOutputStream fos = mContext.openFileOutput(mFileName,Context.MODE_PRIVATE); fos.write(info.getBytes()); fos.close();}catch (Exception e){ e.printStackTrace();}
The first parameter of the openFileOutput () method is used to specify the file name and cannot contain the path delimiter "/". If the file does not exist, Android will automatically create it. openFileOutput () the second parameter of the method is used to specify the operation mode.
The operation modes include:
Context. MODE_PRIVATE = 0: it is the mo-person operation mode. It indicates that the file is private data and can only be accessed by the application itself. In the mode of modification, 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 checks whether the object exists and appends it to the object. Otherwise, a new object is created. Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE are used to control whether other applications have the 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. Androidhas a set of security models. When the application (.apk) is installed, the system will assign a userid to it. When the application wants to access other resources, such as files, it needs to match the userid. By default, files, SharedPrefrences, and databases created by any application must be private and cannot be accessed by other programs. Unless Context. MODE_WORLD_READABLE or Context. MODE_WORLD_WRITEABLE is specified during the creation, other programs can access it normally. 2. Read File Content
try { FileInputStream fis = mContext.openFileInput(mFileName); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); String info = reader.readLine(); fis.close(); return info; }catch (Exception e){ e.printStackTrace(); }
Iii. Case 1. Add the strings. xml 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:id="@+id/file_write" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="@string/write_data" /> <Button android:id="@+id/file_read" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="@string/read_data" /> </LinearLayout>
3. Add the FileDBManager class
package com.zhangmiao.datastoragedemo;import android.content.Context;import android.os.Environment;import android.util.Log;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.ObjectOutputStream;/** * 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 = mContext.openFileOutput(mFileName,Context.MODE_PRIVATE); fos.write(info.getBytes()); fos.close(); }catch (Exception e){ e.printStackTrace(); } } public String read(){ try { FileInputStream fis = mContext.openFileInput(mFileName); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); String info = reader.readLine(); fis.close(); return info; }catch (Exception e){ e.printStackTrace(); } return ""; }}
4. Modify MainActivity
package com.zhangmiao.datastoragedemo;import android.content.ContentResolver;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private FileDBManager mFileManager;private TextView mTableInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFileManager = new FileDBManager(this); mTableInfo = (TextView) findViewById(R.id.table_info); fileWrite.setOnClickListener(this); fileRead.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) {
case R.id.file_write: mFileManager.write("hello world!"); break; case R.id.file_read: mTableInfo.setText(mFileManager.read()); break;default:break; } }}