android文件的写入与读取---简单的文本读写

先贴上程序最后的运行效果图吧:

点击save会保存到文件中,点击show会从文件中读取出内容并显示。

102331_6o3g_2391602.png

文件目录结构如下:

103119_D2IR_2391602.png

main_activity.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"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入你要保存的内容:"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="15sp" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请在此处输入内容!" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="save" />
        <Button
            android:id="@+id/show"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="show" />
    </LinearLayout>
    <TextView
        android:id="@+id/showTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/colorAccent"
        android:textSize="15sp" />
</LinearLayout>

Activity.java文件如下:

package com.yaowen.androidfilewriteandread;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView showText;//显示保存内容的控件
    private Button save, show;//保存和显示按钮
    private EditText saveText;//保存内容的输入框
    private String fileName = "test.txt";//保存的文件名
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        saveText = (EditText) findViewById(R.id.editText);
        save = (Button) findViewById(R.id.save);
        show = (Button) findViewById(R.id.show);
        showText = (TextView) findViewById(R.id.showTextView);
        //设置按钮的点击事件
        save.setOnClickListener(this);
        show.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save: {
                saveTextView();//保存文件
                break;
            }
            case R.id.show: {
                showTextView();//读取文件并显示文件内容
                break;
            }
        }
    }
    private void showTextView() {
        try {
            /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的,
             *   public abstract FileOutputStream openFileOutput(String name, int mode)
             *   throws FileNotFoundException;
             * openFileOutput(String name, int mode);
             * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名
             *          该文件会被保存在/data/data/应用名称/files/test.txt
             * 第二个参数,代表文件的操作模式 
             *          MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖
             *          MODE_APPEND  私有   重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件
             *          MODE_WORLD_READABLE 公用  可读
             *          MODE_WORLD_WRITEABLE 公用 可读写
             *  */
            FileInputStream inputStream = this.openFileInput(fileName);
            byte[] bytes = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (inputStream.read(bytes) != -1) {
                arrayOutputStream.write(bytes, 0, bytes.length);
            }
            inputStream.close();
            arrayOutputStream.close();
            String content = new String(arrayOutputStream.toByteArray());
            if (content != null && content.length() > 0) {//这里做了个判断,判断content要有内容
                showText.setText(content);
            } else {
                Toast.makeText(MainActivity.this, "文件内容为空,请修改或者添加内容!",
                        Toast.LENGTH_SHORT).show();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void saveTextView() {
        String content = saveText.getText().toString();
        if (content != null && content.length() > 0) {//这里做了个判断,判断content要有内容
            try {
                FileOutputStream outputStream = openFileOutput(fileName, Activity.MODE_PRIVATE);
                outputStream.write(content.getBytes());
                outputStream.flush();
                outputStream.close();
                Toast.makeText(MainActivity.this, "保存内容成功!", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(MainActivity.this, "请输入在输入框里输入要保存的内容!",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

 然而项目中的其他文件可以默认也是可以自己美化吧,这里就随意了;

关于文件保存的路径可以通过AndroidStudio携带的File Explorer工具进行查看,这里是我的一个截图:

104036_x4rv_2391602.png

这个项目,基本上没什么难点,就是java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流,简单、方便、快捷。

转载于:https://my.oschina.net/yaowen424/blog/531882

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值