1,数据的保存
File file=new File(Environment.getExternalStorageDirectory(), name); try { FileOutputStream fos=new FileOutputStream(file); fos.write(content.getBytes()); fos.close(); Toast.makeText(getApplicationContext(),"保存成功", 200).show(); }catch (Exception e) { Toast.makeText(getApplicationContext(),"保存失败", 200).show(); e.printStackTrace(); } |
2,数据的读取
File file = new File(Environment.getExternalStorageDirectory(), name); try { FileInputStream fis= new FileInputStream(file); BufferedReader br =new BufferedReader(new InputStreamReader(fis)); //读取文档的数据 String content =br.readLine(); //拆分字符串 String [] str =content.split("="); //显示数据 if(!content.isEmpty()){ et_name.setText(str[0]); et_password.setText(str[1]); } } catch (Exception e) { e.printStackTrace(); } } }); |
注意:保存数据记得注册
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE”/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> |
时间: 2024-10-05 07:04:55