android读写文件

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Handler handler = new Handler() {
    @Override
    public void handleMessage(@NonNull Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1) {
            textView.setText((String) msg.obj);
        }
    }
};
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
}

//读文件
private void read() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File file = Environment.getExternalStorageDirectory();
                FileInputStream is = null;
                try {
                    is = new FileInputStream(new File(file, "你和我的倾城时光.txt"));
                    byte[] bytes = new byte[1024 * 8];
                    int length = 0;
                    StringBuffer stringBuffer = new StringBuffer();
                    while ((length = is.read(bytes)) != -1) {
                        stringBuffer.append(new String(bytes, 0, length));
                    }
                    Message message = Message.obtain();
                    message.what = 1;
                    message.obj = stringBuffer.toString();
                    handler.sendMessage(message);
                    Log.i("##", stringBuffer.toString());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }).start();
}

//写文件
private void write() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            //判断SD卡是否准备好
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //获得SD卡路径
                File file = Environment.getExternalStorageDirectory();
                FileOutputStream os = null;
                try {
                    //获取流
                    os = new FileOutputStream(new File(file, "haha.txt"));
                    //写内容
                    os.write("7758521".getBytes());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //关闭资源
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }).start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值