IO流实现登录注册

一、主函数(用户能看到的操作界面及命令执行)

package Demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class 实验 {
	public static void main(String[] args) {
	//用户所看到的界面
		while(true) {
			System.out.println("====《登录系统》====");
			System.out.println("1、登录");
			System.out.println("2、注册");
			System.out.println("3、退出");
			System.out.print("——选择:");
			
			//文本扫描器,输入上面菜单对应的选项
			Scanner in = new Scanner(System.in);
			int number = in.nextInt();
			
			Login_OR_Registration lor = new Login_OR_Registration();
			
			switch (number) {
			case 1:	
					System.out.println();
					System.out.println("登陆中...");
					System.out.print("账号:");
					String user = in.next();
					System.out.print("密码:");
					String password = in.next();
					
					boolean flag = lor.Login(user,password,false);
				
					if(flag) {
						System.out.println("登录成功!!!");
					}else {
						System.out.println("登录失败!!!");
					}
					
					System.out.println();
					break;
			case 2:	
					System.out.println();
					System.out.println("注册中...");
					System.out.print("账号:");
					String user1 = in.next();
					System.out.print("密码:");
					String password1 = in.next();
										
					do{
						//将当前注册的账号给到Login方法中来判断当前文件中是否已被注册
						boolean estimate = true;
						boolean flag2 = lor.Login(user1,password1,estimate);
						
						if(flag2) {
							System.out.println("此账号已注册!!!");
						}else {
							//将账号密码封装起来
							_User ue = new _User(user1, password1);
							lor.Registration(ue);
							System.out.println("注册成功!!!");
						}
					}while(false);
					
					System.out.println();
					break;
			case 3:
					System.out.println("退出成功!!!");
					System.exit(0);
					break;
			default:
					System.out.println("请重新选择!!!");
					break;
			}
		}	
	}
}

二、定义接口(登录方法与注册方法)

//接口里定义两方法:登录、注册
interface connector{
	//登录
	abstract boolean Login(String user,String password,boolean estimate);
	
	//注册
	abstract void Registration(_User ue);
}

三、实现接口

//登录 OR 注册
class Login_OR_Registration implements connector{
	//将账号密码放入文件中,并且为了让文件在加载之前被创建,所有这里写成了Static
	private static String path = "c:"+File.separator+"test"+File.separator+"zhanghao.txt";
	private static File file = new File(path);
		
	//判断这个文件是否存在,不存在就创建一个新的空白文件
	static {
        if (file.exists()) {
        	
        }else {
        	try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("文件创建失败!!!");
			}
        }
    }
	
	//登录账号
	public boolean Login(String user,String password,boolean estimate) {
		//最终值
		boolean flag = false;
		//为true则是判断账号是否被注册,为false则是判断当前账号登录是否成功
		boolean pd =estimate;
		
		//读取文件里的内容
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			//一行一行的读取文件中的内容:----》readLine()
			String line= null;
			while((line = br.readLine())!=null) {
				 // 字符串的分割功能(  根据给定正则表达式的匹配拆分此字符串。----》split("="))
				String[] split = line.split("=");
				
				//为true则是判断账号是否被注册,为false则是判断当前账号登录是否成功
				if(pd) {
					if(split[0].equals(user)) {
						flag =true;
					}
				}else {
					if(split[0].equals(user) && split[1].equals(password)) {
						flag =true;
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("找不到该文件!!!");
		} catch (IOException e) {
			System.out.println("登录失败!!!");
		}finally {
			if(br !=null) {
				try {
					//关闭流
					br.close();
				} catch (IOException e) {
					System.out.println("资源释放失败!!!");
				}
			}
		}
		
		return flag;
	}

	//注册账号
	public void Registration (_User ue) {
		//IO字符输出流:BufferedWriter(先创建输出流为空,因为还不知道往哪儿输出)
		BufferedWriter bw = null;
		try {
			//这里的true并不是为真,而是为了给文件持续添加注册账号,没有它的话会持续覆盖你每次注册的账号密码
			bw = new BufferedWriter(new FileWriter(file,true));
			bw.write(ue.getUser_()+"="+ue.getPassword_());
			//写入一个行分隔符
			bw.newLine();
			//强制刷新
			bw.flush();
		} catch (IOException e) {
			System.out.println("用户注册失败!!!");
		}finally {
			if(bw !=null) {
				try {
					//关闭流
					bw.close();
				} catch (IOException e) {
					System.out.println("释放失败!!!");
				}
			}
		}
	}
}

四、封装类(将当前注册的账号密码封装在此类中)

//封装类
class _User{
	private String user_;
	private String password_;
	
	//有参构造函数
	public _User(String user_, String password_) {
		this.user_ = user_;
		this.password_ = password_;
	}

	public String getUser_() {
		return user_;
	}

	public void setUser_(String user_) {
		this.user_ = user_;
	}

	public String getPassword_() {
		return password_;
	}

	public void setPassword_(String password_) {
		this.password_ = password_;
	}	
}

五、运行结果

在这里插入图片描述

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值