FileInputStream FileOutputStream

来源:互联网 发布:4glte网络优化前景如何 编辑:程序博客网 时间:2024/05/18 03:07

String path=context.getFilesDir().getPath();//应用的路径//String sdpathString=Environment.getExternalStorageDirectory().getPath();//sd卡的路径

写数据

String result=username+"##"+password;String path=context.getFilesDir().getPath();//应用的路径//String sdpathString=Environment.getExternalStorageDirectory().getPath();//sd卡的路径///mnt/sdcard/info.txt   往sd卡存数据要加权限File file=new File(path,"info.txt");//创建输出流FileOutputStream fos;fos = new FileOutputStream(file);fos.write(result.getBytes());fos.close();

读取数据:

String path=context.getFilesDir().getPath();   File file = new File(path,"info.txt");     FileInputStream fiStream;        Map<String, String> maps=new HashMap<>();//用来保存用户名和密码            fiStream = new FileInputStream(file);            BufferedReader bfReader=new BufferedReader(new InputStreamReader(fiStream));            String resule=bfReader.readLine();            //切割            String[] splits=resule.split("##");            String name=splits[0];            String pwdString=splits[1];            //存到map中            maps.put("name",name);            maps.put("pwd", pwdString);            bfReader.close();//关流


0 0