Android-内部存储-外部存储
一、存储结构


1. 内部存储

2. 外部存储

二、 内部存储


三、外部存储




















作业 习题:

四、案例(存储数据)

布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ControlActivity" android:orientation="vertical"> <EditText android:id="@+id/editText_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:layout_marginTop="50dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_save_internal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存(内部存储)" android:textSize="18sp"/> <Button android:id="@+id/button_load_internal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取(内部存储)" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_save_external_root" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存(外存根目录)" android:textSize="18sp"/> <Button android:id="@+id/button_load_external_root" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取(外存根目录)" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_save_external_public" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存(外存共有目录)" android:textSize="18sp"/> <Button android:id="@+id/button_load_external_public" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取(外存共有目录)" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_save_external_private" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存(外存私有目录)" android:textSize="18sp"/> <Button android:id="@+id/button_load_external_private" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="读取(外存私有目录)" android:textSize="18sp"/> </LinearLayout> <Button android:id="@+id/button_clear_editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清空输入框" android:textSize="18sp"/> </LinearLayout>
Activity
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Environment; import android.provider.ContactsContract; import android.telephony.emergency.EmergencyNumber; import android.text.LoginFilter; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class ControlActivity extends AppCompatActivity implements View.OnClickListener { private final String FILE_NAME = "testData.txt"; private final String TAG = "ControlActivity===="; EditText infoText ; Button saveInternalButton; Button loadInternalButton; Button saveExternalRootButton; Button loadExternalRootButton; Button saveExternalPublicButton; Button loadExternalPublicButton; Button saveExternalprivateButton; Button loadExternalprivateButton; Button clearButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_control); initComponent(); } private void initComponent(){ infoText = (EditText)findViewById(R.id.editText_info); saveInternalButton = (Button)findViewById(R.id.button_save_internal); loadInternalButton = (Button)findViewById(R.id.button_load_internal); saveExternalRootButton = (Button)findViewById(R.id.button_save_external_root); loadExternalRootButton = (Button)findViewById(R.id.button_load_external_root); saveExternalPublicButton = (Button)findViewById(R.id.button_save_external_public); loadExternalPublicButton = (Button)findViewById(R.id.button_load_external_public); saveExternalprivateButton = (Button)findViewById(R.id.button_save_external_private); loadExternalprivateButton = (Button)findViewById(R.id.button_load_external_private); clearButton = (Button)findViewById(R.id.button_clear_editText); saveInternalButton.setOnClickListener(this); loadInternalButton.setOnClickListener(this); saveExternalRootButton.setOnClickListener(this); loadExternalRootButton.setOnClickListener(this); saveExternalPublicButton.setOnClickListener(this); loadExternalPublicButton.setOnClickListener(this); saveExternalprivateButton.setOnClickListener(this); loadExternalprivateButton.setOnClickListener(this); clearButton.setOnClickListener(this); } @Override public void onClick(View view) { String info = ""; switch (view.getId()){ case R.id.button_save_internal: saveToInternalStorage(infoText.getText().toString()); Toast.makeText(this,"消息存储成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_load_internal: info = loadFromInternalStorage(); infoText.setText(info); Toast.makeText(this,"消息获取成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_save_external_root: saveToExternalRootStorage(infoText.getText().toString()); Toast.makeText(this,"消息存储成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_load_external_root: info = loadFromExternalRootStorage(); infoText.setText(info); Toast.makeText(this,"消息获取成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_save_external_public: saveToExternalPublicStorage(infoText.getText().toString()); Toast.makeText(this,"消息存储成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_load_external_public: info = loadFromExternalPublicStorage(); infoText.setText(info); Toast.makeText(this,"消息获取成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_save_external_private: saveToExternalPrivateStorage(infoText.getText().toString()); Toast.makeText(this,"消息存储成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_load_external_private: info = loadFromExternalPrivateStorage(); infoText.setText(info); Toast.makeText(this,"消息获取成功,消息是 "+infoText.getText().toString(),Toast.LENGTH_SHORT).show(); break; case R.id.button_clear_editText: infoText.setText(""); break; } } /** * 将 info 存入路径为 filePath 的文件中 * @param file * @param info */ private void saveToAnyFile(File file, String info) throws IOException { FileOutputStream out = null; if (!file.exists()) { file.createNewFile(); } // 获得文件输出流 out = new FileOutputStream(file.getAbsoluteFile()); out.write(info.getBytes()); try { if (out != null) out.close(); } catch (Exception e) { e.printStackTrace(); } } private String loadFromAnyFile(File file) throws IOException { StringBuffer res = new StringBuffer();// 存放数据 // 打开文件输入流 FileInputStream in = new FileInputStream(file.getAbsoluteFile()); // 准备装载数据的容器 byte[] buffer = new byte[1024]; int len = in.read(buffer); // 不断读取文件内容 while(len >0){ res.append(new String(buffer,0,len)); len = in.read(buffer); } // 关闭输入流 in.close(); return res.toString(); } /** * 将数据存储到文件中(内部存储)(方法一) * @param info */ private void saveToInternalStorage(String info){ File filesDir = getFilesDir(); String filesPath = filesDir.getAbsolutePath(); File txtFile; try { txtFile = new File(filesPath,FILE_NAME); saveToAnyFile(txtFile,info); Log.d(TAG,"写入内部存储目录的文件路径:"+txtFile.getAbsoluteFile()); } catch (IOException e) { e.printStackTrace(); } } private String loadFromInternalStorage() { String res = ""; try { File filesDir = getFilesDir(); File txtFile = new File(filesDir.getAbsolutePath(),FILE_NAME); res = loadFromAnyFile(txtFile); Log.d(TAG,"读取内部存储目录的文件路径:"+txtFile.getAbsoluteFile()); } catch (IOException e) { e.printStackTrace(); } return res; } /** * 写入外部根目录 * @param info */ private void saveToExternalRootStorage(String info){ try { // 检查外围设备是否存在 String enviroment = Environment.getExternalStorageState(); Log.d(TAG," Environment.getExternalStorageState():"+enviroment); if(Environment.MEDIA_MOUNTED.equals(enviroment)) { // 外部设备可以进行读写操作 // 得到外部设备的根目录 File filesDir = Environment.getExternalStorageDirectory(); File txtFile = new File(filesDir,FILE_NAME); Log.d(TAG,"写入外部根目录的文件路径:"+txtFile.getAbsoluteFile()); saveToAnyFile(txtFile, info); } } catch (IOException e) { e.printStackTrace(); } } private String loadFromExternalRootStorage(){ String res = ""; try { // 检查外围设备是否存在 String enviroment = Environment.getExternalStorageState(); Log.d(TAG," Environment.getExternalStorageState():"+enviroment); if(Environment.MEDIA_MOUNTED.equals(enviroment)) { // 外部设备可以进行读写操作 // 得到外部设备的根目录 File filesDir = Environment.getExternalStorageDirectory(); File txtFile = new File(filesDir,FILE_NAME); Log.d(TAG,"读取外部根目录的文件路径:"+txtFile.getAbsoluteFile()); res = loadFromAnyFile(txtFile); } } catch (IOException e) { e.printStackTrace(); } return res; } /** * 写入外部存储共有目录 * @param info */ private void saveToExternalPublicStorage(String info){ try { // 检查外围设备是否存在 String enviroment = Environment.getExternalStorageState(); Log.d(TAG," Environment.getExternalStorageState():"+enviroment); if(Environment.MEDIA_MOUNTED.equals(enviroment)) { // 外部设备可以进行读写操作 // 得到外部设备的根目录 File filesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); File txtFile = new File(filesDir,FILE_NAME); Log.d(TAG,"写入外部存储共有目录的文件路径:"+txtFile.getAbsoluteFile()); saveToAnyFile(txtFile, info); } } catch (IOException e) { e.printStackTrace(); } } private String loadFromExternalPublicStorage(){ String res = ""; try { // 检查外围设备是否存在 String enviroment = Environment.getExternalStorageState(); Log.d(TAG," Environment.getExternalStorageState():"+enviroment); if(Environment.MEDIA_MOUNTED.equals(enviroment)) { // 外部设备可以进行读写操作 // 得到外部设备的根目录 File filesDir = Environment.getExternalStoragePublicDirectory("mydata"); File txtFile = new File(filesDir,FILE_NAME); Log.d(TAG,"读取外部存储共有目录的文件路径:"+txtFile.getAbsoluteFile()); res = loadFromAnyFile(txtFile); } } catch (IOException e) { e.printStackTrace(); } return res; } /** * 写入外部存储私有目录 * @param info */ private void saveToExternalPrivateStorage(String info){ try { File filesDir = getExternalFilesDir("mydata"); File txtFile = new File(filesDir.getAbsolutePath(),FILE_NAME); saveToAnyFile(txtFile,info); Log.d(TAG,"写入外部存储私有目录的文件路径:"+txtFile.getAbsoluteFile()); } catch (IOException e) { e.printStackTrace(); } } private String loadFromExternalPrivateStorage(){ String res = ""; try { File filesDir = getExternalFilesDir("mydata"); File txtFile = new File(filesDir.getAbsolutePath(),FILE_NAME); res = loadFromAnyFile(txtFile); Log.d(TAG,"读取外部存储私有目录的文件路径:"+txtFile.getAbsoluteFile()); } catch (IOException e) { e.printStackTrace(); } return res; } }



浙公网安备 33010602011771号