练习1
package lx2;import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;public class demo1 {/*需求:写一个登陆小案例。步骤:将正确的用户名和密码手动保存在本地的userinfo.txt文件中。保存格式为:username=zhangsan&password=123让用户键盘录入用户名和密码比较用户录入的和正确的用户名密码是否一致如果一致则打印登陆成功如果不一致则打印登陆失败*/public static void main(String[] args) throws IOException {//读取文件中的数据并且添加到集合当中BufferedReader br = new BufferedReader(new FileReader("..\\day51\\src\\lx2\\userinfo.txt"));String line = br.readLine();ArrayList<String> list = new ArrayList<>();list.add(line);System.out.println(list);//[username=zhangsan&password=123]//我们现在要将list集合当中的数据进行拆分//第一次以&为分割String[] arr = line.split("&");//其实0索引是username=zhangsan 1索引是password=123//我们现在第二次以=分割 其中0索引是username 1索引是zhangsan ,0--->password=,1---->123String rightUsername = arr[0].split("=")[1];String rightPassword = arr[1].split("=")[1];System.out.println("请输入用户名");Scanner sc = new Scanner(System.in);String userName = sc.next();System.out.println("请输入密码");String password = sc.next();if (userName.equals(rightUsername) && password.equals(rightPassword)) {System.out.println("登陆成功");} else {System.out.println("登陆失败");}}
}
练习2
package lx2;import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;/*需求:写一个登陆小案例(添加锁定账号功能)步骤:将正确的用户名和密码手动保存在本地的userinfo.txt文件中。保存格式为:username=zhangsan&password=123&count=0让用户键盘录入用户名和密码比较用户录入的和正确的用户名密码是否一致如果一致则打印登陆成功如果不一致则打印登陆失败,连续输错三次被锁定*/
public class demo2 {public static void main(String[] args) throws IOException {//初始化数据writeInfo("username=zhangsan&password=123&count=0");//读取文件中的数据并且添加到集合当中BufferedReader br = new BufferedReader(new FileReader("..\\day51\\src\\lx2\\userinfo.txt"));String line = br.readLine();ArrayList<String> list = new ArrayList<>();//定义一个count,用来计算连续输错的次数int count = 0;list.add(line);System.out.println(list);//[username=zhangsan&password=123]//我们现在要将list集合当中的数据进行拆分//第一次以&为分割String[] arr = line.split("&");//其实0索引是username=zhangsan 1索引是password=123//我们现在第二次以=分割 其中0索引是username 1索引是zhangsan ,0--->password=,1---->123String rightUsername = arr[0].split("=")[1];String rightPassword = arr[1].split("=")[1];while (true) {System.out.println("请输入用户名");Scanner sc = new Scanner(System.in);String userName = sc.next();System.out.println("请输入密码");String password = sc.next();if (userName.equals(rightUsername) && password.equals(rightPassword) && count < 3) {System.out.println("登陆成功");//把数据写入到本地文件当中// username=zhangsan&password=123&count=0writeInfo("username=" + rightUsername + "&password=" + rightPassword + "&count=0");break;} else {count++;if (count < 3) {System.out.println("登陆失败,还剩下" + (3 - count) + "次机会");} else {System.out.println("用户账户被锁定");return;}// username=zhangsan&password=123&count=3writeInfo("username=" + rightUsername + "&password=" + rightPassword + "&count=" + count);}}}/** 作用:* 写出一个字符串到本地文件中* 参数:* 要写出的字符串* */private static void writeInfo(String s) throws IOException {BufferedWriter bw = new BufferedWriter(new FileWriter("..\\day51\\src\\lx2\\userinfo.txt"));bw.write(s);bw.flush();bw.close();}}