关于缓冲区存储的账号和密码的问题
怎么判断读取的是账号还是密码,又怎么用逗号来区分啊
- package 子类窗体包;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JOptionPane;
- import 父类窗体包.AbstractRegistFrame;
- import 用户包.User;
- public class RegistFrame extends AbstractRegistFrame {
- private static final String userPwd1 = null;
- // 关闭按钮
- @Override
- protected void butClose() {
- // 显示登录界面
- new LoginFrame().setVisible(true);
- // 关闭本界面
- this.dispose();
- }
- // 确定按钮
- @Override
- protected void butOK() {
- // 1.获取窗体数据
- String userName = this.txtUserName.getText();
- String userPwd1 = new String(this.txtPwd.getPassword());
- String userPwd2 = new String(this.txtPwd2.getPassword());
- //2.验证用户名和密码是否为空
- if(userName.trim().length() == 0){
- JOptionPane.showMessageDialog(this, "请输入用户名");
- return;
- }
- if(userPwd1.trim().length() == 0){
- JOptionPane.showMessageDialog(this, "请输入用户密码!");
- return;
- }
- if(userPwd2.trim().length() == 0){
- JOptionPane.showMessageDialog(this, "请输入确认密码!");
- return;
-
- }
- //3.验证用户名是否相同
- try {
- List<User> userList = getAllUser();
- for(int i = 0;i < userList.size();i++){
- User u = userList.get(i);
- if(u.getLoginName().equals(userName)){
- JOptionPane.showMessageDialog(this, "用户名" + userName + "已被使用,请指定其他用户名!");
- return;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- //4.两次输入的密码必须一致
- if(!userPwd1.equals(userPwd2)){
- JOptionPane.showMessageDialog(this, "两次输入的密码不一致!");
- return;
- }
- //5.封装对象
- User user = new User(userName,userPwd1);
- //6.将对象封装到集合中
- try {
- writerUser(user);
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "在写入用户信息时发生异常,请于系统管理员联系!");
- return;
- }
- //7.弹出对话框提示用户注册成功
- JOptionPane.showMessageDialog(this, "恭喜您注册成功!");
- //显示登陆界面
- new LoginFrame().setVisible(true);
- //关闭本界面
- this.dispose();
- }
-
- private void writerUser(User user) throws IOException {
- //文件输出流
- Writer out = new FileWriter("user",true);
- //输出数据
- out.write("\r\n" + user.getLoginName()+","+user.getLoginPwd());
- //释放资源
- out.close();
- }
- private List<User> getAllUser() throws IOException {
- //字符输入流
- FileReader in = new FileReader("user");
- List<User> userList = new ArrayList<>();
- int n = 0;
- //存储一行的数据
- StringBuffer buf = new StringBuffer();
- while((n = in.read()) != -1){
- //将读取的数据转换成字符类型存储在字符串缓冲区
- char c = (char)n;
- if(c=='\r'){
- continue;
- }
- if(c == '\n'){
- //判断缓冲区内是否有数据
- if(buf.length() != 0){
- //取出缓冲区中的数据,转换为字符串
- String row = buf.toString();
- //将字符串使用逗号切割
- String[] strArray = row.split(",");
- String userName = strArray[0];
- String userPwd = strArray[1];
- //封装成User对象
- User user = new User(userName,userPwd);
- //将User对象添加到集合中
- userList.add(user);
- //清空StringBuffer
- buf.delete(0,buf.length());
- }
- continue;
- }
- buf.append(c);
- }
- if(buf.length() != 0){
- //取出缓冲区中的数据,转换为字符串
- String row = buf.toString();
- //将字符串使用逗号切割
- String[] strArray = row.split(",");
- String userName = strArray[0];
- String userPwd = strArray[1];
- //封装成User对象
- User user = new User(userName,userPwd);
- //将User对象添加到集合中
- userList.add(user);
- //清空StringBuffer
- buf.delete(0,buf.length());
- }
- //关闭流
- in.close();
- //将List返回
- return userList;
- }
- }
复制代码
|