android-数据存储之内部存储

以下都要抛出异常

保存

一、获取输出流(这里为追加模式)
二、写入数据(写入数据以byte类型保存)
三、刷新数据
四、关闭输出流

 try {
    
    
                    FileOutputStream myFiel = openFileOutput("MyFiel", MODE_APPEND);
                    myFiel.write(et.getText().toString().trim().getBytes());
                    myFiel.flush();
                    myFiel.close();
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }

读取

一、获取输入流(读我们保存的文件)
二、读取数据
读取数据需要byte类型,那就来一个
三、关闭输入流
四、将读取的数据给一个字符变量
五、给控件赋值

try {
    
    
                    FileInputStream myFile = openFileInput("MyFiel");
                    byte[] by = new byte[myFile.available()];
                    myFile.read(by);
                    myFile.close();
                    String data = new String(by);
                    tv.setText(data);
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }

猜你喜欢

转载自blog.csdn.net/Willow_Spring/article/details/112999226
今日推荐