服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java使用IO模拟注册登录

Java使用IO模拟注册登录

2022-11-21 14:14_路人_ Java教程

这篇文章主要为大家详细介绍了Java使用IO模拟注册登录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

user的pojo类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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层接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
    }
 
}

控制台模拟注册登录

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.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;
            }
        }
    }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lg346426260/article/details/65935968

延伸 · 阅读

精彩推荐