简体   繁体   English

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

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

I am learning Java EE and am trying to scan multiple text files based on the option value listed in my code. 我正在学习Java EE,并尝试根据代码中列出的选项值扫描多个文本文件。 If one of the three options is selected it will scan a corresponding text file and output some information. 如果选择了三个选项之一,它将扫描相应的文本文件并输出一些信息。

As you can see I have it working to scan one file while obviously ignoring the option selection. 如您所见,我显然可以忽略一个选项而只扫描一个文件。 I am not sure how to adjust my code to switch to a different file based on the selection. 我不确定如何根据选择将代码调整为切换到其他文件。

I think I will have to move some things around such as moving the while loop inside of an if statement that implements the option choice, and then scan the file for the id which shouldn't be an issue. 我想我将不得不四处移动,例如在实现选项选择的if语句内移动while循环,然后在文件中扫描ID,这应该不是问题。 I am just kind of dumbfounded on reading from different txt files. 我只是从不同的txt文件读取时傻眼了。

I have posted my code below so you can get an idea of what I am trying to do. 我已经在下面发布了我的代码,因此您可以了解我要做什么。

Should I create multiple scanners and implement it that way or should I do something else? 我应该创建多个扫描仪并以这种方式实施它还是应该做其他事情?

Edit: Thanks to a little bit of help I have managed to get it working. 编辑:感谢一点帮助,我设法使它开始工作。 Here is the revised working code. 这是修改后的工作代码。

<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>

Assuming your listed JSP's name is ScoreFinder.jsp and you are submitting records to the same JSP. 假设您列出的JSP的名称为ScoreFinder.jsp,并且您正在将记录提交到同一JSP。

You can create the name of text file from the value of parameter id eg 您可以从参数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