内部及外部SD卡存储空间存储

package com.zdsoft.file1206;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name, et_content;
    private Button bt_write, bt_read, bt_write_sd, bt_read_sd;

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

        initView();
        listener();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_content = (EditText) findViewById(R.id.et_content);
        bt_write = (Button) findViewById(R.id.bt_write);
        bt_read = (Button) findViewById(R.id.bt_read);
        bt_write_sd = (Button) findViewById(R.id.bt_write_sd);
        bt_read_sd = (Button) findViewById(R.id.bt_read_sd);
    }

    private void listener() {
        bt_write.setOnClickListener(this);
        bt_read.setOnClickListener(this);
        bt_write_sd.setOnClickListener(this);
        bt_read_sd.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_write:
                write();
                break;
            case R.id.bt_read:
                read();
                break;
            case R.id.bt_write_sd:
                writeSD();
                break;
            case R.id.bt_read_sd:
                readSD();
                break;
            default:
                break;
        }

    }

    private void write() {
        try {
            FileOutputStream fos = openFileOutput(et_name.getText().toString(), Context.MODE_APPEND);
            fos.write(et_content.getText().toString().getBytes());
            fos.close();
            Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void read() {
        try {
            FileInputStream fis = openFileInput(et_name.getText().toString());
            byte[] b = new byte[1024];
            int num = 0;
            StringBuffer sb = new StringBuffer();
            while ((num = fis.read(b)) != -1) {
                sb.append(new String(b, 0, num));

            }
            Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
            fis.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void writeSD() {


        try {
//            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//                Toast.makeText(MainActivity.this, "内存卡不存在", Toast.LENGTH_SHORT).show();
//                return;
//            }

            File file = new File(Environment.getExternalStorageDirectory() + "/" + et_name.getText().toString());
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(et_content.getText().toString().getBytes());
            fos.close();
            Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void readSD() {
        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/" + et_name.getText().toString());
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int num = 0;
            StringBuffer sb = new StringBuffer();
            while ((num = fis.read(b)) != -1) {
                sb.append(new String(b, 0, num));

            }
            Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
            fis.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
加SD卡权限
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值