gpt4 book ai didi

java - 读取文件时无限循环

转载 作者:行者123 更新时间:2023-11-30 04:10:56 24 4
gpt4 key购买 nike

这是我试图从文件中读取的代码。我的文件当前有 4 行,但读取这些行后代码并未退出循环。

    public class ViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
System.out.println("Logging in..");
// retrieving values entered in form
String uname = request.getParameter("uname");
String pwd = (String) request.getParameter("pwd");

if (uname.equals("admin") && pwd.equals("admin")) {

try {
viewDetails();
System.out.println("Redirecting to view page..");
response.sendRedirect("view.jsp");
} catch (Exception e) {

}
}
}

public void viewDetails() throws Exception{
System.out.println("Reading Data..");
BufferedReader input = new BufferedReader(new FileReader("H:/Workspace/teamRecordsApp/WebContent/records.txt"));
String record;
String[] split =new String[5];

ArrayList cuid = new ArrayList();
ArrayList fname = new ArrayList();
ArrayList lname = new ArrayList();
ArrayList mobile = new ArrayList();
ArrayList desk = new ArrayList();
int count = 0;
while((record=input.readLine()) != null){
split = record.split(",");
System.out.println("Record is : "+split[0]+","+split[1]+","+split[2]+","+split[3]+","+split[4]);
cuid.add(split[0]);
fname.add(split[1]);
lname.add(split[2]);
mobile.add(split[3]);
desk.add(split[4]);
count = count+1;

}
input.close();
System.out.println("Reading Done...");
Utils utils = new Utils();
utils.setCuid(cuid);
utils.setFname(fname);
utils.setLname(lname);
utils.setMobile(mobile);
utils.setDesk(desk);
utils.setCount(count);
System.out.println("Total records : "+count);


}

}

*现在添加了整个 servlet 类。

<小时/>

控制台显示:

Logging in..
Reading Data..
Record is : 12,e,e,1234567890,1234
Record is : 12,e,e,1234567890,1234
Record is : 12,e,e,1234567890,1234

记录为:12,e,e,1234567890,123

在文件中添加 printStackTrace() 后,我收到的异常附在下面。正如@Pshemo 所说,最后一行空行给出了这个异常。我无法通过查看文件并添加 printStackTrace 来解决这个问题。非常感谢@Pshemo 和其他人的帮助!

java.lang.ArrayIndexOutOfBoundsException: 1
at ViewServlet.viewDetails(ViewServlet.java:54)
at ViewServlet.doPost(ViewServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

最佳答案

由于您在 try-catch block 中调用 viewDetails(); ,但您不处理异常(您甚至不打印其堆栈跟踪)

} catch (Exception e) {
//nothing here... :/
}

我假设在“Reading Done...”按摩之前,您的循环中抛出了异常。异常可能是由很多原因引起的,例如如果您的文件有一些空行,您的循环将读取它 "" 并将其拆分为 , 这将创建一个元素数组{""} 然后使用 split[1] 你可能会得到 IndexOutOfBoundException。但这只是其中一种可能性。要确定您的代码有什么问题,请添加

e.printStackTrace(); 

到您的catch (Exception e) block 。

关于java - 读取文件时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com