💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
Android设备上的所有应用都有一个放置在沙盘中的文件目录,将文件保存到沙盒中可以阻止其他应用的访问。 沙盒目录的全路径为:/data/data/<包名>  用File Explorer查看: ![](https://box.kancloud.cn/2016-02-17_56c4396c35105.jpg) 如上图可见,每个应用都在/data/data下有一个以此应用包名命名的文件目录。 而本文就是介绍将文件保存在/data/data/<包名>/files/ 目录下 下面就展示如何在内部存储设备中存储和加载本地文件: 1、创建一个名为 DataStorage的工程 2、准备好布局文件(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="保存数据" /> <Button android:id="@+id/load_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载数据" /> </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); /* 参数一: 文件名。 * 如果文件不存在,Android会自动创建它。创建的文件保存在/data/data/<package name>/files目录下 * 参数二: 文件操作模式参数。代表该文件是私有数据,只能被应用本身访问。 * */ Writer writer = new OutputStreamWriter(out); try { String str = "来自保存在内部存储设备的数据"; 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("没有发现保存的数据"); } finally { reader.close(); } } } ~~~ 运行程序,点击保存数据按钮,Toast显示保存成功字样后。再点击加载数据按钮: ![](https://box.kancloud.cn/2016-02-17_56c4396c45d11.jpg) 可以发现保存在内部存储设备的设备被加载后在TextView显示。再看文件具体位置: ![](https://box.kancloud.cn/2016-02-17_56c4396c5532c.jpg) `#DONE#`