package com.example.testapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class InternalActivity extends ApplicationActivity{
EditText edt;
TextView txt;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal);
edt = findViewById(R.id.editText);
txt = findViewById(R.id.textView7);
}
public void operate(View view){
File f = new File(getFilesDir(),"getFilesDirs.txt");
switch (view.getId()){
case R.id.save_btn2:
try {
if (!f.exists()) {
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(f);
fos.write(edt.getText().toString().getBytes());
fos.close();
}catch (IOException e){
e.printStackTrace();
}
break;
case R.id.read_btn2:
try {
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[1024];
int len = fis.read(b);
String str2 = new String(b,0,len);
txt.setText(str2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxLines="12"
android:minLines="12"
android:hint="请输入待存储的内容..."
android:gravity="left|top"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/read_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="读取"
android:onClick="operate" />
<Button
android:id="@+id/save_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="保存"
android:onClick="operate" />
</LinearLayout>
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="176dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
/>
</LinearLayout>