1.外部存储空间
1.1FileWriteActivity.java
package com.example.chapter06; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import com.example.chapter06.util.FileUtil; import com.example.chapter06.util.ToastUtil; import java.io.File; public class FileWriteActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_name; private EditText et_age; private EditText et_height; private EditText et_weight; private CheckBox ck_married; private String path; private TextView tv_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file_write); et_name = findViewById(R.id.et_name); et_age = findViewById(R.id.et_age); et_height = findViewById(R.id.et_height); et_weight = findViewById(R.id.et_weight); ck_married = findViewById(R.id.ck_married); tv_text = findViewById(R.id.tv_text); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_read).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: String name = et_name.getText().toString(); String age = et_age.getText().toString(); String height = et_height.getText().toString(); String weight = et_weight.getText().toString(); StringBuilder sb = new StringBuilder(); sb.append("姓名:").append(name); sb.append("\n年龄:").append(age); sb.append("\n身高:").append(height); sb.append("\n体重:").append(weight); sb.append("\n婚否:").append(ck_married.isChecked() ? "是" : "否"); String fileName = System.currentTimeMillis() + ".txt"; String directory = null; // 外部存储的公共空间 // directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); // 内部存储私有空间 directory = getFilesDir().toString(); path = directory + File.separatorChar + fileName; Log.d("ning", path); FileUtil.saveText(path, sb.toString()); ToastUtil.show(this, "保存成功"); break; case R.id.btn_read: tv_text.setText(FileUtil.openText(path));/*1350*/ break; } } }
1.2FileUtil.java
package com.example.chapter06.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileUtil { // 把字符串保存到指定路径的文本文件 public static void saveText(String path, String txt) { BufferedWriter os = null; try { os = new BufferedWriter(new FileWriter(path)); os.write(txt); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 从指定路径的文本文件中公读取内容字符串 public static String openText(String path) { BufferedReader is = null; StringBuilder sb = new StringBuilder(); try { is = new BufferedReader(new FileReader(path)); String line = null; while((line = is.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } }
1.3activity_file_write.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="姓名:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:background="@drawable/editext_selector" android:hint="请输入姓名" android:inputType="text" android:maxLength="12" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="年龄:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_age" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:background="@drawable/editext_selector" android:hint="请输入年龄" android:inputType="number" android:maxLength="2" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_height" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="身高:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_height" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:background="@drawable/editext_selector" android:hint="请输入身高" android:inputType="number" android:maxLength="3" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_weight" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="体重:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_weight" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:background="@drawable/editext_selector" android:hint="请输入体重" android:inputType="numberDecimal" android:maxLength="5" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <CheckBox android:id="@+id/ck_married" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:gravity="center" android:text="已婚" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取" android:textColor="@color/black" android:textSize="17sp" /> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>
1.4效果:
保存到外部存储的公共空间,控制台输出:
D/ning: /storage/emulated/0/Download/1667391285238.txt
效果:
文件内容:
点“读取”,效果:
同时,SD卡路径下也有这个文件:
卸载APP后,可以看到,存储在外部存储公共空间的文件仍存在:
保存到内部存储私有空间,控制台日志:
D/ning: /data/user/0/com.example.chapter06/files/1667392095069.txt
效果:
文件内容:
点击“读取”:
卸载APP后,可以发现内部存储私有空间的文件也没了:
标签:11,sb,String,学习,2022,import,et,id,append From: https://www.cnblogs.com/pingfanliliang/p/16849777.html