数据存储
申请权限:略
MainActivity.java
package com.example.saveinfile;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
EditText et_fileName;
EditText et_fileContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//内部存储
public void Button_SaveInterOnclick(View view){
et_fileName = (EditText) findViewById(R.id.ET_FileName);
et_fileContext = (EditText) findViewById(R.id.ET_FileContext);
//要保存的信息所在的文件名
// trim() 方法用于删除字符串的头尾空白符。
String saveFileName = et_fileName.getText().toString().trim();
//要保存的内容
String saveFileContext = et_fileContext.getText().toString().trim();
//准备保存信息 输出流用于写入内容
FileOutputStream outputStream = null;
try {
//打开文件
outputStream = openFileOutput(saveFileName, Context.MODE_PRIVATE);
//写入信息
outputStream.write(saveFileContext.getBytes());
//关闭流
outputStream.close();
Toast.makeText(MainActivity.this,"写入internal成功",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,"写入internal失败",Toast.LENGTH_SHORT).show();
}
}
//内部读取
public void Button_ReadExterOnlick(View view){
TextView ShowInterContext = findViewById(R.id.InterContext);
et_fileName = (EditText) findViewById(R.id.ET_FileName);
et_fileContext = (EditText) findViewById(R.id.ET_FileContext);
String readFileName = et_fileName.getText().toString().trim();
//写入流 可以用于读取数据
FileInputStream inputStream = null;
try{
//打开文件
inputStream = openFileInput(readFileName);
//准备存放打开的文件
byte[] context = new byte[inputStream.available()];
//把流读入字节数据里面
inputStream.read(context);
String stringContext = new String(context);
ShowInterContext.setText( getDir(readFileName,MODE_PRIVATE)+":"+stringContext);
//把流进行关闭
inputStream.close();
}
catch (Exception e ){
}
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
//在指定文件目录下创建一个文件夹
public File getExternalDir(String DirName){
File createDir = Environment.getExternalStoragePublicDirectory(DirName);
if(!createDir.mkdir()){
Log.d("","CreateFail");
}
return createDir;
}
//外部存储
public void Button_SaveExterOnlick(View view){
// trim() 方法用于删除字符串的头尾空白符。
String saveFileName = et_fileName.getText().toString().trim();
//要保存的内容
String saveFileContext = et_fileContext.getText().toString().trim();
et_fileName = (EditText) findViewById(R.id.ET_FileName);
et_fileContext = (EditText) findViewById(R.id.ET_FileContext);
if(isExternalStorageReadable()){
//在指定位置创建文件夹
File createDir = getExternalDir("dir");
//判断文件夹是否创建成功
if(createDir!=null){
File file = new File(createDir,saveFileName);
FileOutputStream outputStream = null;
try{
//打开
outputStream = new FileOutputStream(file);
outputStream.write(saveFileContext.getBytes());
outputStream.close();
Toast.makeText(MainActivity.this,"写入External成功",Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"写入External失败",Toast.LENGTH_SHORT).show();
}
}
}
}
//读取外部
public void Button_ReadExterOnclick(View view){
TextView ShowExterContext = findViewById(R.id.ExterContext);
//判断一下外部的存储的可用性,是否可以进行读操作
if(isExternalStorageWritable()){
//获取文件所在文件夹的位置
File dirName = getExternalDir("dir");
if(dirName!=null){
//准备获取读取的文件内容
//找到读取的文件名字
String readFileName = et_fileName.getText().toString().trim();
File filePath = new File(dirName,readFileName);
FileInputStream inputStream = null;
try {
//将指定文件的完整路径(包括文件),读入到数据流中
inputStream = new FileInputStream(filePath);
//将数据流读入到二进制数组中
byte [] context = new byte[inputStream.available()];
inputStream.read(context);
String fileContext = new String(context);
ShowExterContext.setText(filePath+":"+fileContext);
}catch (Exception e){
}
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingRight="15dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="66dp"
android:paddingRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/TV_FileNameLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="26sp"
android:text="文件名字 :"
android:layout_height="match_parent"></TextView>
<EditText
android:id="@+id/ET_FileName"
android:textSize="26sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="66dp"
android:paddingRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/TV_FileContextLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="26sp"
android:text="文件内容 :"
android:layout_height="match_parent"></TextView>
<EditText
android:id="@+id/ET_FileContext"
android:textSize="26sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="66dp"
android:orientation="horizontal">
<Button
android:id="@+id/SaveInter"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="26sp"
android:text="存入内部"
android:onClick="Button_SaveInterOnclick"
android:layout_height="match_parent"></Button>
<Button
android:id="@+id/SaveExter"
android:textSize="26sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="Button_SaveExterOnlick"
android:text="存入外部"
android:layout_height="match_parent"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="66dp"
android:orientation="horizontal">
<Button
android:id="@+id/ReadInter"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="读取内部"
android:onClick="Button_ReadExterOnlick"
android:textSize="26sp"></Button>
<Button
android:id="@+id/ReadExter"
android:textSize="26sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="Button_ReadExterOnclick"
android:text="读取外部"
android:layout_height="match_parent"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textSize="20sp"
android:gravity="center"
android:id="@+id/InterContextLabel"
android:layout_weight="1"
android:text="内部内容:"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
<TextView
android:textSize="20sp"
android:gravity="center"
android:id="@+id/InterContext"
android:layout_weight="1"
android:text="Hello!"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textSize="20sp"
android:gravity="center"
android:id="@+id/ExterContextLabel"
android:layout_weight="1"
android:text="外部内容:"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
<TextView
android:textSize="20sp"
android:gravity="center"
android:id="@+id/ExterContext"
android:layout_weight="1"
android:text="Hello!"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</LinearLayout>
</LinearLayout>