先贴代码
else if (ae.getSource() == login) {
try {
FileInputStream fis = new FileInputStream("login.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(isr);
String str = null;
while (in.readLine() != null) {
str = in.readLine();
String username = str.split(" ")[0];
String password = str.split(" ")[1];
if (username.equals(tfname.getText())
&& password.equals(tfpassword.getText())) {
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
} else {
System.out.print("error");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} }
用户名和密码是写在login.txt文件里面的,用户名和密码在同一行,用空格隔开,一共存了3组,现在写这段代码只能验证到第二组用户名和密码,输入其他两组都是打印error,我不知道是哪里出了问题,希望有人指出
else if (ae.getSource() == login) {
try {
FileInputStream fis = new FileInputStream("login.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(isr);
String str = null;
while (in.readLine() != null) {
str = in.readLine();
String username = str.split(" ")[0];
String password = str.split(" ")[1];
if (username.equals(tfname.getText())
&& password.equals(tfpassword.getText())) {
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
} else {
System.out.print("error");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} }
用户名和密码是写在login.txt文件里面的,用户名和密码在同一行,用空格隔开,一共存了3组,现在写这段代码只能验证到第二组用户名和密码,输入其他两组都是打印error,我不知道是哪里出了问题,希望有人指出
public String readLine()
throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 返回:
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
抛出:
IOException - 如果发生 I/O 错误
while ((str = in.readLine()) != null)
str = in.readLine(); //不要,读了两次
username2 password2
username3 password3
是这样的格式存在txt文本里面的
str = in.readLine();
}
读了两次改成这样
while ((str=in.readLine()) != null) {
}
else if (ae.getSource() == login) {
try {
FileInputStream fis = new FileInputStream("login.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(isr);
String str = null;
while ((str = in.readLine()) != null) {
String username = str.split(" ")[0];
String password = str.split(" ")[1];
if (username.equals(tfname.getText())
&& password.equals(tfpassword.getText())) {
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
} else {
System.out.print("error");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
&& password.equals(tfpassword.getText())) {
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
} else {
System.out.print("error");
break;
}break;跳出循环,不论是否找到相同的都跳出。
try {
FileInputStream fis = new FileInputStream("login.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader in = new BufferedReader(isr);
String str = null;
str = in.readLine();
while (str != null) {
String username = str.split(" ")[0];
String password = str.split(" ")[1];
if (!username.equals(tfname.getText())
&& !password.equals(tfpassword.getText())) {
// System.out.print("cannot match");
str = in.readLine();
} else if (username.equals(tfname.getText())
&& password.equals(tfpassword.getText())) {
in.close();
LauchWin lw = new LauchWin();
lw.lauchWin();
this.setVisible(false);
break;
}
}