import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*
* 1.在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,
* 并存入data.txt中,要求一个验证码占一行;
2.键盘录入一个需要被校验的验证码,如果输入的验证码在data.txt中存在:
在控制台提示验证成功,如果不存在控制台提示验证失败
*/
public class Test {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("data.txt");
BufferedWriter bw = new BufferedWriter(fw);
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String s01 = sc.nextLine();
System.out.println("请输入字符串:");
String s02 = sc.nextLine();
System.out.println("请输入字符串:");
String s03 = sc.nextLine();
bw.write(s01);
bw.newLine();
bw.flush();
bw.write(s02);
bw.newLine();
bw.flush();
bw.write(s03);
bw.newLine();
bw.flush();
bw.close();
System.out.println("请输入验证码:");
String s = sc.nextLine();
FileReader fr = new FileReader("data.txt");
BufferedReader br = new BufferedReader(fr);
String t = null;
while((t = br.readLine()) != null) {
if(t.equals(s)) {
System.out.println("验证成功");
return;
}
}
System.out.println("验证失败...");
}
}
|
|