文件目录
#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;
}
}
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;
}
}
video
new