IO流读取存储登陆系统
简单模拟其系统化的数据读取与存储,实现其登陆功能,新手学习勿喷.......
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Scanner; 7 8 public class Loading { 9 10 /** 11 * @param args 12 */ 13 //--------登录系统 14 15 static Scanner keyScanner = new Scanner(System.in); 16 17 public static void main(String[] args) throws IOException { 18 // TODO Auto-generated method stub 19 20 System.out.println("----------------------"); 21 System.out.println("-------注册登录系统-------"); 22 System.out.println("----------------------"); 23 24 //---判断是否是第一次注册 25 boolean isTrue = true; 26 27 LoadingCD(isTrue); 28 } 29 30 //----------注册界面 31 public static void LoadingCD(boolean isTrue) throws IOException{ 32 33 System.out.println("*******欢迎来到注册系统*******"); 34 35 System.out.println("请输入你的账户:"); 36 String userString = keyScanner.next(); 37 38 System.out.println("请输入你的密码:"); 39 String passWordString = keyScanner.next(); 40 41 42 43 //----写入数据到磁盘 44 readIn(userString,passWordString,isTrue); 45 } 46 47 //-----------写入数据 48 public static void readIn(String useString,String passwordString,boolean isTrue) throws IOException { 49 50 File file = new File("C:\Users\mac\Desktop\LoadingInformation.txt"); 51 file.createNewFile(); 52 53 System.out.println(isTrue); 54 //---实现重复输入的拼接 55 FileWriter fileWriter = new FileWriter(file,true); 56 57 //-----判断是否重复 58 String isRepitString = useString + " " + passwordString; 59 FileReader fileReader = new FileReader(file); 60 BufferedReader bufferedReader = new BufferedReader(fileReader); 61 String isTrueString = ""; 62 63 if (isTrue) { 64 65 isTrue = false; 66 fileWriter.write(useString + " " + passwordString + " "); 67 fileWriter.close(); 68 System.out.println("注册成功!!!!"); 69 }else { 70 71 while ((isTrueString = bufferedReader.readLine()) != null) { 72 73 if (isRepitString.equals(isTrueString)) { 74 75 System.out.println("用户名已存在,无法注册!!!"); 76 return; 77 }else { 78 79 fileWriter.write(useString + " " + passwordString + " "); 80 //----关闭资源 81 fileWriter.close(); 82 System.out.println("注册成功!!!!"); 83 break; 84 } 85 } 86 } 87 88 //--------提醒操作 89 System.out.println("友情提示:----退出(0)---- 注册(1)---- 登录(2)----"); 90 int chiose = keyScanner.nextInt(); 91 if (chiose == 0) { 92 93 System.out.println("退出成功!!!"); 94 System.exit(0); 95 }else if(chiose == 1){ 96 97 LoadingCD(isTrue); 98 }else if(chiose == 2){ 99 100 loadIN(file); 101 }else { 102 103 System.out.println("操作有误,系统自动退出~~"); 104 return; 105 } 106 } 107 108 //--------登录 109 public static void loadIN(File file) throws IOException{ 110 System.out.println("-----登录界面-----"); 111 System.out.println("登录账户:"); 112 String loadUser = keyScanner.next(); 113 System.out.println("登录密码:"); 114 String loadPassword = keyScanner.next(); 115 FileReader fileReader = new FileReader(file); 116 117 //----拼接你输入的信息按照磁盘的格式 118 String newString = loadUser + " " + loadPassword; 119 120 //----逐行取出数据进行比较 121 BufferedReader bufferedReader = new BufferedReader(fileReader); 122 String str = null; 123 for (int i = 0;(str = bufferedReader.readLine()) != null; i++) { 124 125 if (newString.equals(str)) { 126 127 System.out.println("登录成功!!!!!!"); 128 System.out.println("(>_<)(>_<)(>_<)(>_<)(>_<)(>_<)(>_<)"); 129 return; 130 } 131 } 132 133 System.out.println("登录失败!!请注册或确认信息后重试!!!"); 134 } 135 }