模拟登陆、注册---java代码,输入输出流

 登陆例子

    /*
 * 模拟登陆注册功能
 * 用户输入用户名、密码与文件中的用户信息进行匹配。
 *
 * 文件:config.properties
 * 存储数据的特点:每一行是一个用户信息
 * scott=tiger
 * admin=123456
 *
 */
public class Test01 {
    static File file = null;
    static{
        //加载文件
        try {
            file = new File("config.properties");
            //第一次注册时,文件被创建出来
            if(!file.exists()){
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("1:注册-----2:登录");
        int num = scan.nextInt();
        try {
            String message ="";
            if(num==1){
                message = register(scan);
            }else if(num==2){
                message = login(scan);
            }
            System.out.println(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 登陆功能
     * @param scan:扫描类对象
     * @return 是否登陆成功的提示信息
     * @throws Exception
     */
    public static String login(Scanner scan) throws Exception{
        System.out.println("请输入用户名:");
        String username = scan.next();
        System.out.println("请输入密码:");
        String password = scan.next();
        /*
         * 获取文件中的所有用户信息
         */
        Map<String,String> users = readToMap(file);
        if(!users.containsKey(username)){
            return "没有此用户名";
        }
        String pwd = users.get(username);
        if(!pwd.equals(password)){
            return "密码不正确";
        }
        return "登录成功";
    }
    /**
     * 注册功能
     * @param scan:扫描类对象
     * @return 注册是否成功的提示信息
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
    public static String register(Scanner scan) throws Exception{
        System.out.println("请输入用户名:");
        String username = scan.next();
        System.out.println("请输入密码:");
        String password = scan.next();
        /*
         * 读取文件中的用户信息,封装成Map对象
         * 用户名为key
         * 密码为value
         */
        Map<String,String> users = readToMap(file);
        /*
         * 查看users中是否有username
         */
        if(users.containsKey(username)){
            return "用户名已经存在";
        }
        /*
         * 能注册,保存用户信息,写入文件
         */
        PrintWriter pw =
            new PrintWriter(//构造器
                    new OutputStreamWriter(//字符流
                            new FileOutputStream(file,true),"utf-8"));//字节流
        pw.println(username+"="+password);
        pw.close();
        return "注册成功";
    }
    public static Map<String,String> readToMap(File file) throws Exception{
        Map<String,String> users =
            new HashMap<String,String>();
        BufferedReader br =
            new BufferedReader(//构造器
                    new InputStreamReader(//字符流
                            new FileInputStream(file),"utf-8"));//字节流
        String line = "";                              ,true
        while((line=br.readLine())!=null){
            //分析line
            String[] a = line.split("=");
            //存入users中
            users.put(a[0], a[1]);
        }
        br.close();
        return users;
    }
}



猜你喜欢

转载自blog.csdn.net/xiaozelulu/article/details/80134736