起航学习网

- 让每个人都能学到最前沿新知识、新技能!
起航学习网
当前位置: 起航学习网 > 短期培训 > 编程语言 > 使用Java文件登录注册

使用Java文件登录注册

时间:2022-08-14 14:43:19来源:零基础学Java 作者:Java学习网 已有: 名学员访问该课程

  快捷搜索:java登录注册

前言: 如何使用Java文件登录注册?IT培训网小编来告诉大家。 import java.util.Scanner;import java.io.BufferedWriter;import java.io.File;im

如何使用Java文件登录注册?IT培训网小编来告诉大家。

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
public class Registration {	
	private void register() throws FileNotFoundException
	{
		Scanner sc=new Scanner(System.in);		
		System.out.println("Enter User Name: ");
		String Uname=sc.nextLine();
		System.out.println(Uname);		
		System.out.println("Enter Password: ");
		String Pass=sc.nextLine();
		System.out.println(Pass);		
		System.out.println("Confirm Password: ");
		String ConPass=sc.nextLine();
		System.out.println(ConPass);
		Uname=Uname.trim();
		Pass=Pass.trim();
		ConPass=ConPass.trim();
		String x= Uname+" "+Pass;
		if(Pass.equals(ConPass))
		{
  File f = new File("Registration.txt");
		      Scanner content = new Scanner(f);		      		      
		      int flag=0;
		      while (content.hasNextLine()) {
		        String data = content.nextLine();
		        if(data.equals(x))
		        {
		        	System.out.println("Already Registered");
		        	flag=1;
		        	System.out.println("1. Registration. ");
		System.out.println("2. Login. ");		
		System.out.println("Enter your Choice");
		int choice=sc.nextInt();
		if(choice==1)
		{
this.register();
		}
		else if(choice==2)
		{
this.login();
		}
		else
		{
System.out.println("Choose Proper Option");
		}
		        	break;
		        }
		        content.close();
		      }
		        if(flag==0)
		        {
		        	try {
            BufferedWriter out = new BufferedWriter(new FileWriter("Registration.txt", true)); 
            out.write(Uname+" "+Pass+"\n");
            out.close();
        }
        catch (IOException e) {
        	System.out.println("exception occoured" + e);
        }
		
		System.out.println("Successfully Registered");
		System.out.println("Please login");
		this.login();
		        }
		}
		else
		{
System.out.println("Recheck");
System.out.println("1. Registration. ");
System.out.println("2. Login. ");
System.out.println("Enter your Choice");
int choice=sc.nextInt();
if(choice==1)
{
	this.register();
}
else if(choice==2)
{
	this.login();
}
else
{
	System.out.println("Choose Proper Option");
}
		}
		sc.close();
	}	
	public void login()
	{		
		Scanner sc=new Scanner(System.in);		
		System.out.println("Enter User Name: ");
		String Uname=sc.nextLine();
		System.out.println(Uname);		
		System.out.println("Enter Password: ");
		String Pass=sc.nextLine();
		System.out.println(Pass);
		Uname=Uname.trim();
		Pass=Pass.trim();
		String x= Uname+" "+Pass;	
		try {
		      File f = new File("Registration.txt");
		      Scanner content = new Scanner(f);
		      int flag=0;
		      while (content.hasNextLine()) {
		        String data = content.nextLine();
		        if(data.equals(x))
		        {
		        	System.out.println("Login Successful");
		        	System.out.println("Welcome to the Application.");
		        	flag=1;
		        	break;
		        }
		      }
		        if(flag==0)
		        {
		        	System.out.println("Login Failed");
		        	System.out.println("1. Registration. ");
		System.out.println("2. Login. ");		
		System.out.println("Enter your Choice");
		int choice=sc.nextInt();
		if(choice==1)
		{
this.register();
		}
		else if(choice==2)
		{
this.login();
		}
		else
		{
System.out.println("Choose Proper Option");
		}
		        }		      
		      content.close();
		    } 
catch (FileNotFoundException e) {		    		    	
		      System.out.println("Error.");
		      e.printStackTrace();
		    }		
		sc.close();
	}	
	public static void main(String[] args) throws FileNotFoundException{
try {
		      File obj = new File("Registration.txt");		      
		      if (obj.createNewFile()) 
		      {
		        System.out.println("File is created");
		      } 		      
		    } 
catch (IOException e) 
{
		      System.out.println("An error occurred.");
		      e.printStackTrace();		      
		    }		
		int choice;
		Scanner sc=new Scanner(System.in);
		System.out.println("1. Registration. ");
		System.out.println("2. Login. ");		
		System.out.println("Enter your Choice");
		choice=sc.nextInt();
		sc.nextLine();		
		if(choice==1)
		{
Registration user = new Registration();
user.register();
		}
		else if(choice==2)
		{
Registration user = new Registration();
user.login();
		}
		else
		{
System.out.println("Choose Proper Option");
		}
		sc.close();
	}	
}

输出:

登记。
登录。
输入您的选择
1
输入用户名:
Yash Jain
Yash Jain
输入密码:
abcd
abcd
确认密码:
abcd
abcd
成功注册
请登录
输入用户名:
Yash Jain
Yash Jain
输入密码:
abcd
abcd
登录成功
欢迎使用该应用程序。

 

文章出自:http://qh.itpxw.cn/peixun/software/2022123621.html

文章标题:使用Java文件登录注册



免责声明:本站文章均由入驻起航学习网的会员所发或者网络转载,所述观点仅代表作者本人,不代表起航学习网立场。如有侵权或者其他问题,请联系举报,必删。侵权投诉

你也许会喜欢如下的文章?
(责任编辑:深圳学历教育网)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
培训学校
IT培训网 访问该机构站点 报名留言 加为好友 用户等级:注册会员 用户级别:10 机构名称:IT培训网 联 系 人:罗老师 联系电话:13783581536 联系手机:13783581536 在线客服:起航学习网客服 在 线 QQ:起航学习网客服 电子邮件: 网站域名:http://www.itpxw.cn 注册时间:2016-07-18 11:07 最后登录:2024-02-20 13:02
推荐内容