Java使用IO模拟注册登录(登陆java)

网友投稿 240 2022-08-02


本文实例为大家分享了java使用IO模拟注册登录的具体代码,供大家参考,具体内容如下

user的pojo类

package cn.lg.pojo;

public class User {

private String username;

private String password;

public String getUsername() {

return username;

}

public void setUsername(String username) {

http:// this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public User(String username, String password) {

super();

this.username = username;

this.password = password;

}

}

Dao层接口

package cn.lg.dao;

import cn.lg.pojo.User;

public interface UserDao {

/**

* 注册

* @param uesr

* @return

*/

boolean register(User user);

/**

* 登录

* @param user

* @return

*/

boolean login(User user);

}

Dao实现

package cn.lg.daoImpl;

http://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 cn.lg.dao.UserDao;

import cn.lg.pojo.User;

public class UserDaomImpl implements UserDao {

// 使用静态变量和静态代码块,为了保证文件一加载就创建

private static File file = new File("user.txt");

static {

try {

file.createNewFile();

} catch (IOException e) {

System.out.println("创建文件失败");

// e.printStackTrace();

}

}

@Override

public boolean register(User user) {

boolean flag = false;

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter(file, true));// 追加

bw.write(user.getUsername() + "=" + user.getPasswqpXGKfpGOjord());

bw.newLine();

bw.flush();

flag = true;

} catch (IOException e) {

// e.printqpXGKfpGOjStackTrace();

System.out.println("注册失败");

} finally {

if (bw != null) {

try {

bw.close();

} catch (IOException e) {

// e.printStackTrace();

System.out.println("用户注册释放资源失败");

}

}

}

return false;

}

@Override

public boolean login(User user) {

boolean flag = false;

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(file));

String line = null;

while ((line = br.readLine()) != null) {

String[] datas = line.split("=");

if (datas[0].equals(user.getUsername()) && datas[1].equals(user.getUsername())) {

flag = true;

break;

}

}

} catch (FileNotFoundException e) {

System.out.println("用户登录找不到信息所在的文件");

//e.printStackTrace();

} catch (IOException e) {

System.out.println("用户登录失败");

//e.printStackTrace();

}finally {

if(br!=null){

try {

br.close();

} catch (IOException e) {

System.out.println("用户登录释放资源失败");

//e.printStackTrace();

}

}

}

return flag;

}

}

控制台模拟注册登录

package cn.lg.main;

import java.util.Scanner;

import cn.lg.dao.UserDao;

import cn.lg.daoImpl.UserDaomImpl;

import cn.lg.pojo.User;

/**

* @author L

* @date 2017年3月25日 上午11:36:31

*

*/

public class Main {

public static void main(String[] args) {

while (true) {

System.out.println("-----------welcome-----------");

System.out.println("1 登录");

System.out.println("2 注册");

System.out.println("3 退出");

Scanner in = new Scanner(System.in);

String choice = in.nextLine();

// 调用Dao层

UserDao userDao = new UserDaomImpl();

switch (choice) {

case "1":// 登录

System.out.println("------------登录界面-----------");

System.out.println("请输入账户:");

String username = in.nextLine();

System.out.println("请输入密码:");

String password = in.nextLine();

boolean flag = userDao.login(new User(username, password));

if (flag) {

System.out.println("登录成功");

} else {

System.out.prqpXGKfpGOjintln("登录失败");

}

break;

case "2":

System.out.println("------------注册界面-----------");

System.out.println("请输入账户:");

String newUsername = in.nextLine();

System.out.println("请输入密码:");

String newPassword = in.nextLine();

userDao.register(new User(newUsername, newPassword));

System.out.println("注册成功");

break;

case "3":

break;

default:

System.out.println("谢谢使用,欢迎下次再来");

System.exit(0);

break;

}

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java模拟实现斗地主的洗牌和发牌(java斗地主发牌)
下一篇:mybatis plus常用注解的具体使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~