实现注册,登录 注册功能:输入自己的个人信息,保存到文件。 登录:输入自己的个人信息,与文件中的每行用户信息比较,判断登录是否成功.

目录

简单的一个登录程序

上代码

简陋的登录界面

送一个小礼物:


简单的一个登录程序

          这是一个简单的将账号密码存在map里面方便验证登录的小程序,通过输入的account和键equals,输入的password和值equals,这样我们可以轻松判断账号和密码是否正确,只是账号密码才内存中储存,一旦程序终止,数据将被清空。上代码处的代码,我们将数据储存在磁盘中,这样将会一直保存下来,我们下次还可以继续登录,保证用户信息不会丢失。


package Lx22.Day0708;

import java.util.HashMap;
import java.util.Scanner;
public class Homework4 {

    public static void main(String[] args) {
        HashMap<String, String> User = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择你的操作/1.注册操作2.登录操作");
            int i = scanner.nextInt();
            if (i == 1) {//注册方法
                System.out.println("请输入账号");
                String account = scanner.next();
                System.out.println("请输入密码");
                String passWord = scanner.next();
                User.put(account, passWord);
            }
            if (i == 2) {//登录方法
                System.out.println("请输入账号");
                String account = scanner.next();
                System.out.println("请输入密码");
                String passWord = scanner.next();
                if (!User.containsKey(account)) {//判断map里面有没有输入的键
                    System.out.println("账号错误或查无此账号");
                }
                else{
                    if(User.get(account).equals(passWord))
                    {
                        System.out.println("登陆成功");
                        break;
                    }
                    else System.out.println("密码错误");
                }
            }
        }
    }
}

上代码

这个程序我们:

创建一个map,将map流入磁盘txt文件内;

map是双列结构,键值对,键不可重复(我们注册账号时可以省去检查申请的账号是否重复)方便验证账号密码登录。

package Lx22.Day0712;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Homework1Map {
     static void login() throws IOException {//注册
        Scanner scanner = new Scanner(System.in);
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 5; i++) {
            System.out.println("输入7 继续注册 输入其他终止注册");
            int a = scanner.nextInt();
            if (a == 7) {
                System.out.println("请输入注册的账号");
                String s1 = scanner.next();
                System.out.println("请输入注册的密码");
                String s2 = scanner.next();
                map.put(s1, s2);//将账号密码存到map里面
            } else break;
        }
        FileOutputStream out = new FileOutputStream("D:\\852\\账号密码.txt");
        ObjectOutputStream Wout = new ObjectOutputStream(out);
        Wout.writeObject(map);//将map流入到磁盘
        out.flush();//刷新流
        out.close();//关闭流
    }

    static void logon() throws IOException, ClassNotFoundException {//登录
        Scanner scanner = new Scanner(System.in);
        FileInputStream reader = new FileInputStream("D:\\852\\账号密码.txt");
        ObjectInputStream oin = new ObjectInputStream(reader);
        Map<String,String> map = (Map<String, String>)oin.readObject();//把map读出来
        System.out.println("请输入账号");
        String z = scanner.next();
        System.out.println("请输入密码");
        String m = scanner.next();
        if (!map.containsKey(z)) {//判断map里面有没有输入的键
            System.out.println("账号错误或查无此账号");
        }
        else{
            if(map.get(z).equals(m))
            {
                System.out.println("登陆成功");
            }
            else System.out.println("密码错误");
        }



    }
}
package Lx22.Day0712;

import java.io.*;
import java.util.Scanner;

public class Homework1 {

    /*2.实现注册,登录。
      用户信息:用户名(userName),密码(password)
     (1)注册功能:输入自己的个人信息,保存到文件。
     (2)登录:输入自己的个人信息,与文件中的每行用户信息比较,判断登录是否成功.*/
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println(" 输入1 进入注册功能");
            System.out.println(" 输入2进入登录功能");
            System.out.println(" 输入3退出");
            int i = scanner.nextInt();
            if (i == 1)
                Homework1Map.login();
            if (i == 2)
                Homework1Map.logon();
            if (i == 3)
                break;
        }
    }

    }


简陋的登录界面

 


送个小礼物:

给定一个目录,遍历此目录中所有的文件(如果有子级目录,也需要遍历其中的所有文件)
 

package Lx22.Day0711io;

import java.io.File;

public class Homework1 {
    public static void main(String[] args) {
        File f1 = new File("D://852");
        sout(f1);
    }

    static void sout(File file) {
        File[] list = file.listFiles();
        for (File f2 : list) {
            if (f2.isFile())
                System.out.println(f2);
            else {
                sout(f2);
            }
        }

    }
}

测试字节流read(),read(byte[] b)  和两个write(),write(byte[] b,int off,int length)的读写速度.    

package Lx22.Day0711io;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Homework2 {
    public static void main(String[] args) throws IOException {
     FileInputStream in1  = new FileInputStream("D:\\852\\1.txt");
     byte [] bi = new byte[2];
     long begin1 = System.currentTimeMillis();
     while(in1.read()!=-1) {
     }
     long end1 = System.currentTimeMillis();
     System.out.println("read耗时"+(end1-begin1)+"ms");
     FileInputStream in2  = new FileInputStream("D:\\852\\1.txt");
        long begin2 = System.currentTimeMillis();
     while (in2.read(bi)!=-1){
     }
        long end2 = System.currentTimeMillis();
        System.out.println("read(byte [])耗时"+(end2-begin2)+"ms");
        System.out.println("---------------------分割-----------------------------------");
        long bgein3 = System.currentTimeMillis();
        try{
            FileInputStream in3 = new FileInputStream("D://852//1.txt");
            FileOutputStream out3 = new FileOutputStream("D://852//2.txt");
            byte b [] = new byte[5];
            int size = 0;
            while((size = in3.read(b))!=-1)
            {
                out3.write(b, 0, size);
            }

        } catch(FileNotFoundException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        long end3 = System.currentTimeMillis();
        System.out.println("write(byte[])耗时"+(end3-bgein3)+"ms");
        long bgein4 = System.currentTimeMillis();

        try{
            FileInputStream in4 = new FileInputStream("D://852//1.txt");
            FileOutputStream out4 = new FileOutputStream("D://852//2.txt");
            int b=0;
            while((b=in4.read())!=-1)
            {
                out4.write(b);
            }

        } catch(FileNotFoundException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        long end4 = System.currentTimeMillis();
        System.out.println("write()耗时"+(end4-bgein4)+"ms");
    }
    }

猜你喜欢

转载自blog.csdn.net/weixin_56800176/article/details/125748270