移动技术开发:文件的读取

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代码。关于写入,将用户输入的信息写入到文件中,创建输出流管道,向文件中写入数据;关于读取,从文件中读取数据,创建一个连接文件的输入流管道和一个存放读取过来的数据是缓存数组,创建可变长度的缓存字符串,记录下读取量。

       这个实验存在些许不足,个人认为主要是在于一次只能读取一个先前写入的内容,这个部分应该还可以继续优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茜茜西西CeCe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值