package com.iouser.pojo; public class User { private int id ; private String name ; private String psw ; private String sex ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package com.iouser.main; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import com.iouser.pojo.User; public class UserDemoMain { private static Scanner sc ; private static BufferedWriter bw ; private static BufferedReader br ; public static void init(){ sc = new Scanner(System.in); try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bw.txt",true),"UTF-8")); br = new BufferedReader(new InputStreamReader(new FileInputStream("bw.txt"),"UTF-8")); } catch (IOException e) { e.printStackTrace(); } } public static void close(){ try{ if(bw!=null){ bw.close(); } if(br!=null){ br.close(); } }catch(IOException e){ e.printStackTrace(); } } public static void main(String[] args) { init(); while(true){ System.out.println("A: 登录"); System.out.println("B: 注册"); System.out.println("Q: 退出"); String in = sc.next(); if("q".equals(in.toLowerCase())){ System.out.println("************正在退出系统!************"); break; }else if("a".equals(in.toLowerCase())){ boolean flag = login(); if(flag){ System.out.println("*********进入系统功能页面**********"); }else{ System.out.println("*********登录失败,请重新选择*******"); } }else if("b".equals(in.toLowerCase())){ register(); }else{ System.out.println("您输入有误,请重新输入"); } } close(); System.out.println("已退出系统,谢谢使用"); } private static void register() { System.out.println("******请输入你的账号信息进行注册***********"); System.out.println("请输入你的账号名称:"); //通过获取文件中已存在的账号信息 /* List<User> list = null; try { list = getAllUser(); } catch (IOException e) { e.printStackTrace(); }*/ String name = sc.next() ; //判断是否存在...省略 System.out.println("请输入你的密码:"); String psw = sc.next(); System.out.println("请输入你的性别:"); String sex = sc.next(); System.out.println("**********正在为你注册*********"); writeUser(name,psw,sex); } private static boolean writeUser(String name, String psw, String sex) { List<User> list = null; try { list = getAllUser(); } catch (IOException e) { e.printStackTrace(); } int size = list.size(); StringBuilder sbr = new StringBuilder(); sbr.append(size+1); sbr.append(","+name); sbr.append(","+psw); sbr.append(","+sex); System.out.println("sbr="+sbr.toString()); try { bw.write(sbr.toString()); bw.newLine(); bw.flush(); } catch (IOException e) { e.printStackTrace(); } return true; } private static boolean login() { System.out.println("********请输入你的账号和密码进行登录*******"); System.out.println("请输入你的账号:"); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println(name); System.out.println("请输入你的密码:"); String pwd = sc.next(); System.out.println(pwd); List<User> list = null; try { list = getAllUser(); } catch (IOException e) { e.printStackTrace(); } if(list!=null && list.size()!=0){ for(User u : list){ if(u.getName().equals(name) && u.getPsw().equals(pwd)){ System.out.println("**********登录成功***********"); return true; } } } return false ; } private static List<User> getAllUser() throws IOException { close(); init(); List<User> list = new ArrayList<User>(); String line = null ; while((line = br.readLine())!=null){ String[] arrays = line.split(","); User user = new User(); user.setId(Integer.parseInt(arrays[0])); user.setName(arrays[1]); user.setPsw(arrays[2]); user.setSex(arrays[3]); list.add(user); } return list; } }