Android之FileOutputStream文件存储

本文探讨了Android中使用FileOutputStream进行文件存储的核心代码,并详细解释了四种不同的文件存储模式:私有模式(MODE_PRIVATE)、追加模式(MODE_APPEND)、全局只读模式(MODE_WORLD_READABLE)和全局只写模式(MODE_WORLD_WRITEABLE)。了解这些模式对于有效地管理和保护应用数据至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件存储是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;
        }
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值