各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!!
1 import java.io.BufferedOutputStream; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.FileReader; 9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.util.Scanner; 12 13 14 public class work2 { 15 16 static Scanner next = new Scanner(System.in); 17 /** 18 * @param args 19 * @throws IOException 20 */ 21 public static void main(String[] args) throws IOException { 22 // TODO Auto-generated method stub 23 while(true){ 24 System.out.println("请进行以下操作! 1.注册 2.登录"); 25 int s = next.nextInt(); 26 switch (s) { 27 case 1: 28 Login(); 29 break; 30 case 2: 31 Logon(); 32 break; 33 34 default: 35 System.out.println("输入错误返回!!"); 36 break; 37 } 38 } 39 } 40 41 public static String SyIn(){ 42 System.out.println("请输入账号!"); 43 String str = next.next(); 44 System.out.println("请输入密码!"); 45 String str2 = next.next(); 46 return str+" "+str2; 47 } 48 49 public static void Logon() throws IOException{ 50 //输入的账号和密码 51 String accountAndPassWork = SyIn(); 52 //文档该在的位置 53 FileReader fin = new FileReader("C:\Users\wang\Desktop\Login.txt"); 54 //字符流输出缓存区 55 BufferedReader reader = new BufferedReader(fin); 56 //文档中获取的账号和密码 57 String gainAccountAndPassWork = null; 58 //账号 的状态 59 boolean Y = false; 60 //让其返回一行一行的字符串 61 while((gainAccountAndPassWork = reader.readLine())!=null){ 62 //判断字符串和密码加起来是否相同 63 if(gainAccountAndPassWork.equals(accountAndPassWork)){ 64 System.out.println("登陆成功!"); 65 Y = true; 66 break; 67 }else{ 68 Y = false; 69 } 70 } 71 //关闭资源! 72 reader.close(); 73 //如果状态是没有就不存在 74 if(Y == false){ 75 System.out.println("该账号不存在或密码错误!!"); 76 } 77 } 78 79 public static void Login() throws IOException{ 80 //输入的账号和密码 81 String accountAndPassWork = SyIn(); 82 //文件的地址 83 File file = new File("C:\Users\wang\Desktop\Login.txt"); 84 //判断文件在不在,不在的话创建 85 if(!file.exists()){ 86 file.createNewFile(); 87 } 88 //读取文件的内容 89 FileReader fileReader = new FileReader(file); 90 //字符流读取缓冲区 91 BufferedReader bufferR = new BufferedReader(fileReader); 92 //给字符读取缓冲器返回的字符设为null 93 String contrast = null; 94 //阿斯克码表的空格是23 95 int As = 32; 96 //将空格转为字符串 97 String kong = ""+(char)As; 98 //获取账号密码的截取 99 String[] Ac = accountAndPassWork.split(kong); 100 //在这里要有遍历所有的账户的举动 101 //不断地读取一行一行的字符串 102 while((contrast = bufferR.readLine())!=null){ 103 //从返回的一行一行的字符串中找取空格并截取 104 String[] strAll = contrast.split(kong); 105 //判断账号是否有其相同,如果有就return 106 if(strAll[0].equals(Ac[0])){ 107 System.out.println("用户名重复"); 108 //关闭资源 109 bufferR.close(); 110 return; 111 } 112 } 113 114 //字符串流 115 FileWriter finWriter = new FileWriter("C:\Users\wang\Desktop\Login.txt",true); 116 //写入缓冲流 117 BufferedWriter buffer = new BufferedWriter(finWriter); 118 //格一行的,换行 119 buffer.newLine(); 120 //写入账号和密码 121 buffer.write(accountAndPassWork); 122 //关闭资源 123 buffer.close(); 124 System.out.println("注册成功!"); 125 } 126 127 }