android开发:保存并且记住账号密码,输入输出流

保存到data里面

方法一:这种保存方法并不提倡使用,因为绝对路径换一个环境可能就不能使用了

		//传过来的参数用##拼接成一个字符串
		String info=name+"##"+password;
//		创建一个文件,用于存放密码账户
		File file=new File("data/data/com.example.test11/info.txt");
		try {
			//文件输出流,用于往内存中写数据
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(info.getBytes());
			fos.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

我理解的输入输出流的方法是:以自己为中心,如果你要向外界,向文档里面写东西,就是你向外边输出,这就是输出流,使用FileOutputStream
从内存里提取出来东西让我知道,是输入,输入流FileInputStream

方法二:使用上下文获取相对路径,安全

String temp=name+"##"+password;
		try {
			FileOutputStream fos = context.openFileOutput("info2.txt",Context.MODE_PRIVATE);
			fos.write(temp.getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
保存到SD卡
String temp=name+"##"+password;
							//获取sd卡的相对路径
		File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/info.txt");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(temp.getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

记住密码,再次登陆时账号和密码能够显示出来

					//保存密码的路径
File file=new File("data/data/com.example.test11/info.txt");

		try {
//			文件输入流,为文本内容从内存里读过来
			FileInputStream fis=new FileInputStream(file);
//			开始从内存里读过来
			BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
			String temp=reader.readLine();
			
			String[] result = temp.split("##");
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

给大家分享一个专注于分享编程知识和有趣案例的公众号:oldCode
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值