AndroidStudio——数据存储之内部存储

来源:互联网 发布:淘宝二手书出售流程 编辑:程序博客网 时间:2024/04/28 14:54

InnerIoActivity布局:

public class InnerIoActivity extends AppCompatActivity {    private Button save, read, delete;    private EditText content;    private TextView show;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_inner_io);        save = (Button) findViewById(R.id.save);        read = (Button) findViewById(R.id.read);        delete = (Button) findViewById(R.id.delete);        content = (EditText) findViewById(R.id.content);        show = (TextView) findViewById(R.id.show);        save.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 调用SAveFile方法  文件保存到date/date目录下                saveFile();            }        });        read.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                show.setText(readFile());            }        });        delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                removeFile();            }        });    }    //删除文件    public void removeFile() {        String[] files = fileList();        for (String str : files) {            if (str.equals("text.text")) {                deleteFile(str);                break;            }        }    }    //从内存储中读取文件    public String readFile() {        // BufferedReader包装流(字符流 字节流)  带缓冲区的        BufferedReader reader = null;        FileInputStream fis = null;        StringBuilder sbd = new StringBuilder();        try {            fis = openFileInput("text.text");            reader = new BufferedReader(new InputStreamReader(fis));            sbd.append(getFilesDir().getCanonicalPath());            String row = "";            while ((row = reader.readLine()) != null) {                sbd.append(row);            }        } catch (FileNotFoundException e) {            Toast.makeText(getBaseContext(), "文件不存在", Toast.LENGTH_SHORT).show();            //e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return sbd.toString();    }    //保存文件到内存储    public void saveFile() {        FileOutputStream fos = null;        try {            /*            openFileOutput返回一个输出字节流            指向的路径为data/data/包名/files            参数1:文件名称(如果不存在则自动创建)            参数2:模式MODE_APPEND文件内容可樶加                  模式MODE_PRIVATE文件内容被覆盖             */            fos = openFileOutput("text.text", MODE_APPEND);            String str = content.getText().toString();            fos.write(str.getBytes());            Toast.makeText(getBaseContext(), "保存成功", Toast.LENGTH_SHORT).show();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fos != null) {                try {                    fos.close();                    fos.flush();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

activity_inner_io布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"    tools:context="com.example.dell.jreduch008.InnerIoActivity">    <EditText        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入内容"        android:layout_alignParentStart="true"        android:layout_alignParentTop="true" />    <Button        android:id="@+id/save"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="保存"        android:layout_below="@+id/content"        android:layout_alignParentStart="true" />    <Button        android:id="@+id/read"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读取"        android:layout_above="@+id/show"        android:layout_centerHorizontal="true" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/show"        android:layout_below="@id/save"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="删除"        android:id="@+id/delete"        android:layout_above="@+id/show"        android:layout_alignParentEnd="true" /></RelativeLayout>
1 0