内部存储案例

package com.ty.innerstore_demo1;

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;
import java.io.IOException;

//openFileInput返回一个指向默认内部存储位置的FileInputStream
//openFileOutput同理
public class MainActivity extends AppCompatActivity {
    String innerPath = null;
    Button btn_Save;
    Button btn_Show;
    EditText et_Input;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_Save = (Button) findViewById(R.id.BTN_Save);
        btn_Show = (Button) findViewById(R.id.BTN_Show);
        et_Input = (EditText) findViewById(R.id.ET_Input);

    }

    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.BTN_Save:
                SaveFile();
                break;
            case R.id.BTN_Show:
                LoadFile();
                break;
        }
    }

    private void LoadFile() {
        FileInputStream fis = null;
        int count = 0;
        byte[] b = new byte[5];
        try {
            fis = openFileInput("Inner.txt");
            while ((count = fis.read(b))!=-1)
            {
                Toast.makeText(this, new String(b,0,count), Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis != null)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void SaveFile() {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("Inner.txt", MODE_APPEND);
            fos.write(et_Input.getText().toString().getBytes());
            fos.flush();
            Toast.makeText(this, "Saved Text", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }





/*自定义内部存储目录的读写
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        innerPath = getFilesDir().getPath() + "/MyDir";
        File file = new File(innerPath);
        if (!file.exists()) {
            file.mkdir();
        }
        btn_Save = (Button) findViewById(R.id.BTN_Save);
        btn_Show = (Button) findViewById(R.id.BTN_Show);
        et_Input = (EditText) findViewById(R.id.ET_Input);
    }

    public void OnClick(View v) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        File filePath = new File(innerPath, "Toast.txt");

        switch (v.getId()) {
            case R.id.BTN_Save:
                try {
                    fos = new FileOutputStream(filePath, true);
                    fos.write(et_Input.getText().toString().getBytes());
                    fos.flush();
                    Toast.makeText(this, "Saved Text", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            case R.id.BTN_Show:
                byte[] b = new byte[1024];
                int count = 0;
                try {
                    fis = new FileInputStream(filePath);
                    while ((count = fis.read(b))!= -1)
                    {
                        Toast.makeText(this, new String(b,0,count), Toast.LENGTH_SHORT).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
*/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值