java 中用IO存储学生的学号和密码 实行注册和登录功能

新人自己手写代码 ,如有问题,请多多指教!


package com.itcast.wang;


import java.util.Scanner;


/*
 * 
 * 学生登录注册页面
 * 
 * */
public class Login {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StudentData sd = new StudentData();
while (true) {
System.out.println("请选择,A登录,B注册");
String s=sc.nextLine();
if (s.equalsIgnoreCase("A")) {
System.out.println("登录中");
System.out.println("请输入学号");
String studentId = sc.nextLine();
System.out.println("请输入密码");
String passworld = sc.nextLine();
if (sd.login(studentId, passworld))
System.out.println("登录成功!");
else{
System.out.println("登录失败,请重新登录!");
continue;
}


} else if (s.equalsIgnoreCase("B")) {
System.out.println("注册中");
System.out.println("请输入学号");
String studentId = sc.nextLine();
System.out.println("请输入密码");
String passworld = sc.nextLine();
sd.register(studentId, passworld);
System.out.println("注册成功!");
continue;


} else {
System.out.println("选择错误,请重新选择");
continue;


}
}
}
}



package com.itcast.wang;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/*存储学生类数据的类*/
public class StudentData {
File file = new File("E:\\StudentData.txt");


public void register(String studentId, String passworld) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write(studentId + "=" + passworld);
bw.newLine();
bw.flush();
bw.close();
} catch (Exception e) {
}
}


public boolean login(String studentId,String passworld){
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {

e.printStackTrace();
}
}
boolean b=false;
try{ BufferedReader br=new BufferedReader(new FileReader(file));
String s=null;
while((s=br.readLine())!=null){
String[] array=s.split("=+");
// System.out.println(array.length);
/*System.out.println(array[0]);
System.out.println(array[1]);*/

if(array[0].equals(studentId)&&array[1].equals(passworld))
b=true;
}
br.close();

}catch(Exception e){}
return b;

}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值