本文实例为大家分享了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) {
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 {
boolean register(User user);
boolean login(User user);
}
Dao实现
package cn.lg.daoImpl;
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.getPassword());
bw.newLine();
bw.flush();
flag = true;
} catch (IOException e) {
// e.printStackTrace();
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;
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.println("登录失败");
}
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;
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。