Android基础之写入、读取数据

这是一个可以实现读取写入内存卡数据
这是界面

public class ReadInterStorage extends AppCompatActivity implements View.OnClickListener{
    private EditText editText_account;
    private EditText editText_key;
    private Button button_login;
    private CheckBox checkBox_remberKey;
    private long blockSize;
    private long availableBlocks;
    Button button_getAvail;
    TextView textView_getAvail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_inter_storage);
        editText_account = (EditText)findViewById(R.id.editText_account);
        editText_key = (EditText)findViewById(R.id.editText_key);
        button_login = (Button)findViewById(R.id.button_login);
        checkBox_remberKey = (CheckBox) findViewById(R.id.checkbox_remberKey);
        button_getAvail = (Button)findViewById(R.id.button_getAvail);
        textView_getAvail = (TextView) findViewById(R.id.textView_avail);
        readAccount();
        button_login.setOnClickListener(this);
        button_getAvail.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_login:
                String name = editText_account.getText().toString();
                String key = editText_key.getText().toString();
                //判断checkBox是否勾选
                if (checkBox_remberKey.isChecked()) {
                    //定义内部存储空间的路径
                    //File file = new File("data/data/com.esioner.practise.ReadStorage/info.txt");
                    //通过api接口获得文件路径
                    // File file = new File(getFilesDir(),"info.txt");
                    //mediaRemove:没有SD卡                       //mediaUnkonw:不能识别SD卡
                    // mediaUnkonwed:SD卡存在但是没有挂载// mediaCheckING:SD卡正在准备
                    // mediaMounted:SD卡已经挂载,可用

                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        //把文件写入到sd卡
                        File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
                        FileOutputStream fos;
                        try {
                            fos = new FileOutputStream(file);
                            fos.write((name + "##" + key).getBytes());
                            fos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                            Toast.makeText(ReadInterStorage.this, "恭喜登陆成功", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(ReadInterStorage.this, "SD卡不可用", Toast.LENGTH_LONG).show();
                        }
                    }
                break;
            case R.id.button_getAvail:
                getAvail();
                Toast.makeText(ReadInterStorage.this,"textView_getAvail",Toast.LENGTH_LONG).show();
                break;
        }
    }
    public void readAccount() {
        //File file = new File("data/data/com.esioner.practise.ReadStorage/info.txt");
        File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
        FileInputStream fis;
        if (file.exists()) {
            try {
                fis = new FileInputStream(file);
                //把字节流转换成字符流
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                //读取文件里的账号和密码
                String text = br.readLine();
                String[] s = text.split("##");
                editText_account.setText(s[0]);
                editText_key.setText(s[1]);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //获取当前手机可用内存
    public void getAvail(){
        //获取当前手机的sd卡内存
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        //获取当前系统版本的等级
        if(Build.VERSION.SDK_INT>=18) {//获取当前手机版本号
            //当版本号大于等于18,(安卓4.3)就用新的方法
            blockSize = stat.getBlockSizeLong();
            availableBlocks = stat.getAvailableBlocksLong();
        }else{
            blockSize = stat.getBlockSize();
            availableBlocks = stat.getAvailableBlocks();
        }
        textView_getAvail.setText(formatSize(availableBlocks*blockSize));
    }
    private String formatSize(long size){
        return Formatter.formatFileSize(this,size);
    }
}

Android自学中,留下笔记。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值