IO流案例:验证码

/*
一、 分析以下需求,并用代码实现:
实现一个验证码小程序,要求如下:

	1. 在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,并存入data.txt中,要求一个验证码占一行;
	2. 键盘录入一个需要被校验的验证码,如果输入的验证码在data.txt中存在:在控制台提示验证成功,如果不存在控制台提示验证失败

*/

public class Demo6 {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);

		// 创建高效的字符输出流
		BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"));

	for (int i = 1; i <= 3; i++) {
		System.out.println("录入第" + i + "个验证码:");
		String s = sc.nextLine();
		bw.write(s);
		bw.newLine();
		bw.flush();
	}

	bw.close();

	System.out.println("录入一个需要被校验的验证码:");
	String ss = sc.nextLine();// bcd

	BufferedReader br = new BufferedReader(new FileReader("data.txt"));

	boolean flag = false;// false 被校验的验证码在文件中不存在
	String line;
	while ((line = br.readLine()) != null) {
		if (line.equals(ss)) {
			flag = true;
		}
	}

	if (flag) {
		System.out.println("验证成功!");
	} else {
		System.out.println("验证失败!");
	}

	br.close();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值