Android文件储存

2007 杨昌荣 11:48:54

第一行代码Android(第2版) 文件储存

1.打开软件创建项目工程。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.找到activity_main.xml和MainActivit.java输入正确代码。
activity_main.xml:
在这里插入图片描述

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="3dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数据的内部存储"
        android:textSize="22sp" />
    <Button
        android:id="@+id/but_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="存储数据"
        android:textSize="22sp"/>
    <Button
        android:id="@+id/but_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据"
        android:textSize="22sp"/>


</LinearLayout>

2007 杨昌荣 11:49:25
MainActivit.java:

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214113429831.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MzA2NTQzMg==,size_16,color_FFFFFF,t_70#pic_center)

```java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * 主界面的逻辑代码
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button but_save,but_read;//保持与读取按钮
    private Object FileInputStream;
    private String fileName = "mydb.text";//文件名称
    // String strNr = "HelloWorld";//要保存的数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but_save = findViewById(R.id.but_save);
        but_read = findViewById(R.id.but_read);


        but_save.setOnClickListener(this);
        but_read.setOnClickListener(this);
    }

    //点击处理方法
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_save://保存数据
                savaData();
                break;
            case R.id.but_read:
                readData();
                break;
        }

    }



    //保存数据的方法
    private void savaData() {
        // String fileName = "mydb.text";//文件名称
        String strNr = "HelloWorld,I'm coming";//要保存的数据
        FileOutputStream fos = null;

        try {
            fos = openFileOutput(fileName,MODE_PRIVATE);
            fos.write(strNr.getBytes());//将数据写入文件中去

            Toast.makeText(this, "保存数据成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (fos!=null){
                    fos.close();
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }

    }


    //读取数据的方法


    private void readData() {
        String strNr = "";//要读取的内容变量
        // FileInputStream = null;
        InputStream fis = null;
        try {
            fis = openFileInput(fileName);//获取输入流对象
            byte[] buffer = new byte[fis.available()];//创建读取的缓冲对象
            fis.read(buffer); //将文件内容读取到缓冲区中去
            strNr = new String(buffer);  //转换成字符串
            Toast.makeText(this, strNr, Toast.LENGTH_SHORT).show();//显示

        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            try {
                if (fis!=null) {
                } else {
                    fis.close();
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }


    }
}

3.点击运行输出如下图。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值