Android設(shè)備上的所有應(yīng)用都有一個放置在沙盤中的文件目錄,將文件保存到沙盒中可以阻止其他應(yīng)用的訪問。
沙盒目錄的全路徑為:/data/data/<包名> 用File Explorer查看:
如上圖可見,每個應(yīng)用都在/data/data下有一個以此應(yīng)用包名命名的文件目錄。
而本文就是介紹將文件保存在/data/data/<包名>/files/ 目錄下
下面就展示如何在內(nèi)部存儲設(shè)備中存儲和加載本地文件:
1、創(chuàng)建一個名為 DataStorage的工程
2、準(zhǔn)備好布局文件(activity_data_storage.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/data_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未操作" />
<Button
android:id="@+id/save_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存數(shù)據(jù)" />
<Button
android:id="@+id/load_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加載數(shù)據(jù)" />
</LinearLayout>
3、DataStorageActivity.java
package com.example.datastorage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DataStorageActivity extends ActionBarActivity {
private static final String FILENAME = "data.txt";
private static final String TAG = "DataStorageActivity";
private TextView dataView;
private Button saveButton;
private Button loadButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_storage);
dataView = (TextView) findViewById(R.id.data_view);
saveButton = (Button) findViewById(R.id.save_button);
loadButton = (Button) findViewById(R.id.load_button);
setListener();
}
private void setListener() {
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
saveData();
} catch (IOException e) {
}
Toast.makeText(DataStorageActivity.this, "保存成功",
Toast.LENGTH_SHORT).show();
}
});
loadButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
loadData();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
});
}
public void saveData() throws IOException {
OutputStream out = this.openFileOutput(FILENAME, Context.MODE_PRIVATE);
/* 參數(shù)一: 文件名。
* 如果文件不存在,Android會自動創(chuàng)建它。創(chuàng)建的文件保存在/data/data/<package name>/files目錄下
* 參數(shù)二: 文件操作模式參數(shù)。代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問。
* */
Writer writer = new OutputStreamWriter(out);
try {
String str = "來自保存在內(nèi)部存儲設(shè)備的數(shù)據(jù)";
writer.write(str);
} finally {
writer.close();
}
}
public void loadData() throws FileNotFoundException, IOException {
BufferedReader reader = null;
StringBuilder data = new StringBuilder();
try {
InputStream in = this.openFileInput(FILENAME);
Log.i(TAG, in.toString());
reader = new BufferedReader(new InputStreamReader(in));
String line = new String();
while ((line = reader.readLine()) != null) {
data.append(line);
}
dataView.setText(data);
} catch (FileNotFoundException e) {
dataView.setText("沒有發(fā)現(xiàn)保存的數(shù)據(jù)");
} finally {
reader.close();
}
}
}
運行程序,點擊保存數(shù)據(jù)按鈕,Toast顯示保存成功字樣后。再點擊加載數(shù)據(jù)按鈕:
可以發(fā)現(xiàn)保存在內(nèi)部存儲設(shè)備的設(shè)備被加載后在TextView顯示。再看文件具體位置:
更多建議: