import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { ZhuCei zc=new ZhuCei(); Den d=new Den(); while(true) { System.out.println("欢迎进入shop"); System.out.println("1是注册"); System.out.println("2是登录"); System.out.println("3是退出"); Scanner sc=new Scanner(System.in); int a=sc.nextInt(); switch (a) { case 1: System.out.println("请输入用户名"); String user = sc.next(); System.out.println("请输入密码"); String password = sc.next(); zc.ZhuCei(user, password); break; case 2: System.out.println("请输入用户名"); user = sc.next(); System.out.println("请输入密码"); password = sc.next(); d.Den(user, password); break; case 3: return; case 4: Guanli gl=new Guanli(); } } } }
import java.io.*; public class Den { public void Den(String user,String password) throws IOException { File s=new File("d:/chaoshi/2.txt"); FileReader fr=new FileReader(s); BufferedReader br=new BufferedReader(fr); String zong=user+"@"+password; while(true){ String line=br.readLine(); if(zong.equals(line)) System.out.println("登录成功"); if(line == null) break; System.out.println(line); } } }
import java.io.*; public class ZhuCei { public void ZhuCei(String user,String password) throws IOException { File f=new File("d:/chaoshi/2.txt"); FileReader fr=new FileReader(f); BufferedReader br=new BufferedReader(fr); int sh=0; while(true) { String line=br.readLine(); if(line==null) break; String str = line; String[] split = str.split("@",10); //split:{"1","2"} if(split[0].equals(user)){ System.out.println("已注册"); sh=1; break; } } fr.close(); if(sh==0) { FileWriter fw = new FileWriter(f, true); PrintWriter bw = new PrintWriter(fw); bw.println(user + "@" + password); bw.println("\n"); fw.close(); } } }