1 实验名称
文件的读写
2 实验目的
掌握Android中读写文件的实现方法。
3 实验源代码
布局文件代码:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/inputET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你想写入的内容"
android:minLines="2"
/>
<Button
android:id="@+id/inputBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"
/>
<EditText
android:id="@+id/outputET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
/>
<Button
android:id="@+id/outputBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
/>
</LinearLayout>
Java代码:
package com.example.fileiotest;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText inputET = null;
private EditText outputET = null;
private Button inputBtn = null;
private Button outputBtn = null;
private String fileName = "content.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputET = findViewById(R.id.inputET);
outputET = findViewById(R.id.outputET);
inputBtn = findViewById(R.id.inputBtn);
outputBtn = findViewById(R.id.outputBtn);
inputBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//将用户输入的信息写入到文件中
String inputContent = inputET.getText().toString().trim();
//创建输出流管道
FileOutputStream fos = openFileOutput(fileName, Context.MODE_APPEND);
//向文件中写入数据
fos.write(inputContent.getBytes());
inputET.setText("");
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
outputBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//从文件中读取数据
//创建一个连接文件的输入流管道
FileInputStream fis = openFileInput(fileName);
//创建一个存放读取过来的数据是缓存数组
byte[] bufffer = new byte[256];
//创建可变长度的缓存字符串
StringBuffer stringBuffer = new StringBuffer("");
int hasRead = 0;//记录读取量
while ((hasRead=fis.read(bufffer)) != -1){
stringBuffer.append(new String(bufffer,0,hasRead));
}
outputET.setText(stringBuffer.toString());
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
}
4 实验运行结果图
5 实验总结
第一步,写布局文件。主要是两个按钮,一个文本框,一个文本编辑框;两个按钮分别是写入和读取。
第二步,写Java代码。关于写入,将用户输入的信息写入到文件中,创建输出流管道,向文件中写入数据;关于读取,从文件中读取数据,创建一个连接文件的输入流管道和一个存放读取过来的数据是缓存数组,创建可变长度的缓存字符串,记录下读取量。
这个实验存在些许不足,个人认为主要是在于一次只能读取一个先前写入的内容,这个部分应该还可以继续优化。