Android 內部存儲

核心代碼

// 保存
            FileOutputStream fos = null;
            try {
                fos = openFileOutput("test.txt", MODE_PRIVATE);
                fos.write(etName.getText().toString().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

// 讀取
            FileInputStream fis = null;
            try {
                fis = openFileInput("test.txt");
                byte[] buff = new byte[1024];
                StringBuilder sb = new StringBuilder();
                int len = 0;
                while ((len = fis.read(buff)) > 0) {
                    sb.append(new String(buff, 0, len));
                }
                tvShow.setText(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章