我正在使用jsp并想检查输入是否存在于整个文本文件的第一列
中假设我有 user.text 并包含
测试,名字,姓氏
Test02,用户,User08
如果用户输入“Test”,它将打印存在。否则它会将输入添加到文件中。
这是我的代码:
<%
String user= request.getParameter("user");
String firstname = request.getParameter("firstname");
String lastname= request.getParameter("lastname");
File file = new File("/user.txt");
BufferedReader read = new BufferedReader(new FileReader(path));
PrintWriter out = new PrintWriter(new FileWriter(file, true));
String line = read.readLine();
if(line.contains(user)) {
%>
<H3>User Already exist...</H3>
<%
}
else{
outt.println(User + "," + firstname + "," + lastname + "\n");
outt.close();
br.close();
%>
<p> Done...</p>
<%
}
%>
它只检查第一行的问题。
答案 0 :(得分:0)
您需要添加一个循环:
<%
String user= request.getParameter("user");
String firstname = request.getParameter("firstname");
String lastname= request.getParameter("lastname");
File file = new File("/user.txt");
BufferedReader read = new BufferedReader(new FileReader(path));
PrintWriter out = new PrintWriter(new FileWriter(file, true));
String line = null;
while((line = read.readLine()) != null) {
if(line.contains(user)) {
%>
<H3>User Already exist...</H3>
<%
break;
}
}
if(line == null)
outt.println(User + "," + firstname + "," + lastname + "\n");
outt.close();
br.close();
%>
<p> Done...</p>
另外,验证(line.contains(user)
)看起来太容易出错......