繁体   English   中英

如何同时扫描多个txt文件?

[英]How do I scan multiple txt files at the same time?

我正在学习Java EE,并尝试根据代码中列出的选项值扫描多个文本文件。 如果选择了三个选项之一,它将扫描相应的文本文件并输出一些信息。

如您所见,我显然可以忽略一个选项而只扫描一个文件。 我不确定如何根据选择将代码调整为切换到其他文件。

我想我将不得不四处移动,例如在实现选项选择的if语句内移动while循环,然后在文件中扫描ID,这应该不是问题。 我只是从不同的txt文件读取时傻眼了。

我已经在下面发布了我的代码,因此您可以了解我要做什么。

我应该创建多个扫描仪并以这种方式实施它还是应该做其他事情?

编辑:感谢一点帮助,我设法使它开始工作。 这是修改后的工作代码。

<body>
    <%
    String id = request.getParameter("id");        
    String cid = request.getParameter("classID");
    String[] title = {"Name: ", "SSN: ", "Score: "};
    Scanner sc;        
    %>

    Find Your Current Score.
    <%-- Blank space here --%>
    <p></p>
    <%-- Input Forms (Text Field and Button) --%>
    <form action="ScoreFinder.jsp" method="get">
    Employee ID:
    <input type="text" name="id"/> Use SSN Format: xxx-xxx-xxxx
    <p></p>
    Class:
    <select name="classID">
    <option value="CMIS440">CMIS 440</option>
    <option value="CMIS445">CMIS 445</option>
    <option value="CMIS485">CMIS 485</option>
    </select>
    <input type="submit" value="Submit" />
    </form>

    <%
        if (id != null || cid != null) {
            String fileName = "/"+cid.replaceAll(" ", "") + ".txt";
            sc = new Scanner(new File(application.getRealPath(fileName)));
            while(sc.hasNext()) {                
            String line = sc.nextLine();
            String[] split = line.split("[#]");
            if (id.length() == 0) {
                out.println("Please enter an ID number.");
                break;
            }
            if (line.contains(id)) {
                out.println("Class: " + cid);
                %><br><%
                for (int i=0; i<split.length; i++){
                    out.println(title[i]);
                    out.println(split[i]);
                    %><br><%
                }
                break;
            }
            }
        }%>
</body>

假设您列出的JSP的名称为ScoreFinder.jsp,并且您正在将记录提交到同一JSP。

您可以从参数id的值创建文本文件的名称,例如

<body>
    <%
    String id = request.getParameter("id");
    String fileName = "/"+id.replaceAll(" ", "")+".txt"; // avoiding null check of 'id' for simplicity
    Scanner sc = new Scanner(new File(application.getRealPath(fileName)));
    try {
        while(sc.hasNext()) {

    ........
    ........

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM