美文网首页
存储数据

存储数据

作者: 鴻9527 | 来源:发表于2019-08-11 21:37 被阅读0次

    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();
    }
    

    }

    相关文章

      网友评论

          本文标题:存储数据

          本文链接:https://www.haomeiwen.com/subject/lscejctx.html