Android之FileOutputStream文件存储

文件存储是Android中最基本的一种数据存储方式,它与Java中的文件存储类似,都是通过 I/O流的形式把数据原封不动的存储到文档中。

文件存储核心代码

保存文件
 FileOutputStream fileOutputStream;
                    fileOutputStream = openFileOutput("data.txt",Context.MODE_APPEND);
                    fileOutputStream.write(edit.getText().toString().trim().getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();

读取文件
FileInputStream fileInputStream;
                    fileInputStream = openFileInput("data.txt");
                    
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    byte[] bufffer = new byte[fileInputStream.available()];
                    int len = 0 ;
                    while((len = fileInputStream.read(bufffer)) != -1){
                        bout.write(bufffer,0,len);
                    }
                    byte[] content = bout.toByteArray();
                    String str = new String(content);
                    Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
                    fileInputStream.close();

4种模式:

1.当前程序读写:

   MODE_PRIVATE

2.追加模式:

   MODE_APPEND

3.其他程序只读:

   MODE_WORLD_READABLE

4.其他程序只写:

  MODE_WORLD_WRITEABLE

package com.example.win7.myapplication;

import android.content.Context;
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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button save_btn,read_btn;
    private EditText edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        save_btn = (Button) findViewById(R.id.save_btn);
        read_btn = (Button)findViewById(R.id.read_btn);

        edit = (EditText) findViewById(R.id.editText);

        save_btn.setOnClickListener(this);
        read_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.read_btn:

                try {
                    FileInputStream fileInputStream;
                    fileInputStream = openFileInput("data.txt");

                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    byte[] bufffer = new byte[fileInputStream.available()];
                    int len = 0 ;
                    while((len = fileInputStream.read(bufffer)) != -1){
                        bout.write(bufffer,0,len);
                    }
                    byte[] content = bout.toByteArray();
                    String str = new String(content);
                    Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
                    fileInputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally {

                }
                break;
            case R.id.save_btn:
            {


                try {
                    FileOutputStream fileOutputStream;
                    fileOutputStream = openFileOutput("data.txt",Context.MODE_APPEND);
                    fileOutputStream.write(edit.getText().toString().trim().getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
                break;

            default:
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28468727/article/details/52749885