標籤:android學習筆記之資料的內部儲存方 資料的內部儲存方式
(1)目錄結構
(2) 布局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="28dp" android:layout_marginTop="17dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="54dp" android:text="儲存資訊" /></RelativeLayout>
(3)儲存資料、讀取資料的工具類:FileService.java
package com.example.data_storage_interal.file;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.content.Context;/** * 使用內部儲存的方式,可以使應用卸載的時候清除所有的相關資訊 * @author piaodangdehun * */public class FileService {private Context context;public FileService(Context context) {this.context = context;}/** * 儲存內容到檔案中 * @param fileName 檔案名稱 * @param mode 模式 * @param data 資料的緩衝區 * @return 返回真假值 */public boolean saveContentToFile(String fileName, int mode, byte[] data) {boolean flag = false;FileOutputStream outputStream = null;try {outputStream = context.openFileOutput(fileName, mode);try {outputStream.write(data, 0, data.length);} catch (IOException e) {e.printStackTrace();}flag = true;} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (outputStream != null) {try {outputStream.close();} catch (Exception e2) {}}}return true;}/** * 讀取資料 * @param fileName * @return */public String readContentFromFile(String fileName) {String result = "";FileInputStream fileInputStream = null;ByteArrayOutputStream outputStream = new ByteArrayOutputStream();try {fileInputStream = context.openFileInput(fileName);int len = 0;byte[] data = new byte[1024];while ((len = fileInputStream.read(data)) != -1) {outputStream.write(data, 0, len);}return new String(outputStream.toByteArray());} catch (Exception e) {e.printStackTrace();}return "";}}
(4)MainActivity.java
package com.example.data_storage_interal;import com.example.data_storage_interal.file.FileService;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private Button button;private EditText editText;private FileService fileService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) this.findViewById(R.id.button1);editText = (EditText) this.findViewById(R.id.editText1);fileService = new FileService(this);/* * 按鈕點擊的時候存放到檔案中 */button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String valueString = editText.getText().toString().trim();boolean flag = fileService.saveContentToFile("bb.txt",Context.MODE_APPEND, valueString.getBytes());if (flag) {Toast.makeText(MainActivity.this, "儲存成功!",Toast.LENGTH_SHORT).show();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
(5)測試類別(要在資源檔中添加相應的資訊、這裡不說)
package com.example.data_storage_interal;import com.example.data_storage_interal.file.FileService;import android.app.Service;import android.content.Context;import android.test.AndroidTestCase;import android.util.Log;public class MyTest extends AndroidTestCase {private static final String TAG = "MyTest";/* * 儲存資料 */public void save() {FileService fileService = new FileService(getContext());boolean flag = fileService.saveContentToFile("aa.txt",Context.MODE_PRIVATE + Context.MODE_APPEND, "nihao".getBytes());Log.i(TAG, "--->>" + flag);}/* * 儲存資料 */public void read() {FileService fileService = new FileService(getContext());String msg = fileService.readContentFromFile("bb.txt");Log.i(TAG, "--->>" + msg);}}
儲存成功後會在data-data-apk的安裝目錄下找到相應的檔案:
Android學習筆記之資料的內部儲存方式實習資料的讀寫