• java实现用户登录


    文件目录

    在这里插入图片描述
    #userDoa

    package doa;//doa操作文档的
    
    import sun.security.util.Password;
    
    import java.io.*;
    
    public class UserDoa {
        //用户的注册
        public static boolean UserRegisrer(String username, String psw) {
            boolean u = false;
            BufferedWriter bf = null;
            try {
                String temp = "";
                bf = new BufferedWriter(new FileWriter("D:\\javad\\java_______________code\\Userlogin\\src\\user.txt"));
                bf.write("username=" + username);
                bf.newLine();
                bf.write("password=" + psw);
                u = true;
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (bf != null) {
                        bf.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return u;
        }
    
        //用户登录
        public static boolean userLogin(String username, String psw) {
            System.out.println("开始登录");
            boolean rst = false;
            BufferedReader br = null;
            String line = null;
            boolean ub = false;
            boolean pb = false;
            try {
                br = new BufferedReader(new FileReader("D:\\javad\\java_______________code\\Userlogin\\src\\user.txt"));
                while ((line = br.readLine()) != null) {
                    if (line.equals("username=" + username))//是名字才会比对。
                    {
                        System.out.println("ERROR1");
    
                        ub = true;
                    }
                    if (line.equals("password=" + psw)) {
                        System.out.println("ERROR2");
    
                        pb = true;
                    }
                }
                if (ub && pb) {
                    System.out.println("成功");
                    rst = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (Exception q) {
                    q.printStackTrace();
                }
            }
    
            return rst;
    
        }
    
        //修改密码
        public static boolean caangepsw(String oldpsw, String psw) {
            FileWriter fw = null;
            BufferedWriter bw = null;
            try {
                fw = new FileWriter("D:\\javad\\java_______________code\\Userlogin\\src\\user.txt", true); // 传一个true,表示添加数据的方式是追加
                bw = new BufferedWriter(fw);
                bw.newLine();
                bw.write("password=" + psw);
                bw.flush();
                System.out.println("修改成功!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bw == null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }
    
    
        //注销
        public static boolean withdraws() {
    
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("D:\\javad\\java_______________code\\Userlogin\\src\\user.txt"));
                bw.write("");
                bw.flush();
                System.out.println("注销成功");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
    
                    try {
                        if (bw == null) {
                            bw.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }return  true;
            }
    
        }
    
    
    
    
    
    
    
    
    • 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
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    userservlet

    package servlet;//方法入口,调用以及交互
    
    import doa.UserDoa;
    
    import java.util.Scanner;
    
    public class UserServlet {
        public static void main(String[] args) {
            //注册
            registered();
            //   登录
                boolean bl = false;
                bl = login();
                if (bl) {
                    System.out.println("登录成功");
    
                } else {
                    System.out.println("登录失败,请修改密码");
                    caangepsws();   //修改秘密
                }
            withdraw();      //注销
        }
          //修改密码
            public static boolean caangepsws()
            {    System.out.println("是否要修改密码,,输入0为否定,其他数字为肯定");
                Scanner input = new Scanner(System.in);//用于接收各种数据类型,需要导入.util包。
                boolean b=false;
                int j = input.nextInt();//接收不同类型的数据
                if(j!=0)
                {
                    String oldpsw = null;
                    String newpsw = null;
                    UserDoa userDoa = new UserDoa();
                    System.out.println("请输入原始密码");
                    Scanner scnne = new Scanner(System.in);
                    oldpsw = scnne.nextLine();
                    System.out.println("请输入新密码");
                    newpsw = scnne.nextLine();
                    b = UserDoa.caangepsw(oldpsw, newpsw);
                }
                else{
                    System.out.println("欢迎下次使用");
                }
                return  true;
            }
          //登录
        public static boolean login() {
            boolean e = false;
            String username = null;
            String psw = null;
            UserDoa userDoa = new UserDoa();
            Scanner scnner = new Scanner(System.in);
            System.out.println("请输入用户名");
    
            username = scnner.nextLine();
            System.out.println("请输入密码");
            psw = scnner.nextLine();
            e = UserDoa.userLogin(username, psw);
            // System.out.println(e);
            return e;
        }
    
        //创建
        public static boolean registered()
        {boolean w=false;
            String username = null;
            String psw = null;
            UserDoa userDoa = new UserDoa();
            Scanner scnner = new Scanner(System.in);
            System.out.println("请输入用户名");
            username = scnner.nextLine();
            System.out.println("请输入密码");
            psw = scnner.nextLine();
            w = UserDoa.UserRegisrer(username, psw);
            return w;
        }
        //注销
        public static   boolean withdraw()
        {        UserDoa userDoa = new UserDoa();
            boolean f=false;
            System.out.println("true 或者false");
            Scanner input = new Scanner(System.in);//用于接收各种数据类型,需要导入.util包。
           boolean j = input.nextBoolean();//接收不同类型的数据
            if(j)
            {
                System.out.println("开始注销");
               f= userDoa.withdraws();
    
            }
            return  true;
    
        }
    }
    
    
    
    
    
    • 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
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    video

    new

  • 相关阅读:
    Smartbi亮相华为云828 B2B企业节,一起成就好生意
    C#Socket
    Lazada、Shopee、亚马逊等跨境平台,如何通过自养号给自己店铺做测评补单?
    Myeclipse反编译插件(jad)的安装和使用
    IB数学与音乐的融合
    SpringBoot实现SSMP整合
    HTML5期末考核大作业、HTML个人主页界面设计源码
    【Linux系统化学习】开发工具——gdb(调试器)
    html静态网站基于游戏网站设计与实现共计10个页面 (仿地下城与勇士游戏网页)
    GFS分布式文件系统
  • 原文地址:https://blog.csdn.net/m0_62953927/article/details/125615871