BufferedReader循环错误

时间:2013-08-15 21:53:36

标签: java bufferedreader

我知道这可能很简单,但我似乎无法找到我做错了什么。

我正在将网页的输出与文本文件的内容进行比较。我有以下代码,似乎对于来自网页的每个结果,bufferedreader循环一次。一旦我发布代码和结果,这将不那么混乱。

代码

String docOne = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div/div/h5")).getAttribute("textContent").toString();
String docTwo = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[2]/div/h5")).getAttribute("textContent").toString();
String docThree = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[3]/div/h5")).getAttribute("textContent").toString();
String docFour = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[4]/div/h5")).getAttribute("textContent").toString();
String docFive = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[5]/div/h5")).getAttribute("textContent").toString();
String docSix = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[6]/div/h5")).getAttribute("textContent").toString();
String docSeven = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[7]/div/h5")).getAttribute("textContent").toString();
String docEight = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[8]/div/h5")).getAttribute("textContent").toString();
String docNine = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div[2]/div/div[2]/div/div/div/h5")).getAttribute("textContent").toString();

try (BufferedReader br = new BufferedReader(new FileReader("/Users/vhaislsalisc/Documents/CDWProductionDomainsRequiredDocs.txt"))) {
    String sCurrentLine;


    while ((sCurrentLine = br.readLine()) != null) {

        System.out.println("Webpage: " + docOne + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docTwo + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docThree + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docFour + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docFive + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docSix + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docSeven + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docEight + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docNine + " Required Doc: " + sCurrentLine);
    }
    br.close();
} catch (IOException e) {
    System.err.println("Error: " + e.getMessage());
}

结果

Webpage: Research Request Memo (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Study Institutional Review Board (IRB) Approval Letter (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Sample Informed Consent and HIPAA Authorization (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research and Development (RD) Committee Approval Letter (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Data Use Agreement (File Document) Required Doc: Research Request Memo (File Document)
Webpage: IRB Approval of Waiver of HIPAA-Compliant Authorization (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Protocol (File Document) Required Doc: Research Request Memo (File Document)
Webpage: CDW-Domain Checklist (File Document) Required Doc: Research Request Memo (File Document)
Webpage: VA Form 9957 (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Request Memo (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research Study Institutional Review Board (IRB) Approval Letter (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Sample Informed Consent and HIPAA Authorization (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research and Development (RD) Committee Approval Letter (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Data Use Agreement (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: IRB Approval of Waiver of HIPAA-Compliant Authorization (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research Protocol (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: CDW-Domain Checklist (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: VA Form 9957 (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)

通过文件的其余部分继续

所以只是想知道我做了什么noob的事情,以及我如何解决它所以它只通过文件一次,所有内容都符合网页的内容?

1 个答案:

答案 0 :(得分:2)

它会遍历文档文件中每一行的docOne到docNine的每个网页。即它将所有九个文件比较为第1行,然后比较第2行,等等。

由于您已粘贴从文本文件中读取的前两行,并且它们与前两个网页相匹配,因此我假设您想要读取每一行并将其与一个网页进行比较。你需要做的就是丢失while循环并在显示每个结果后调用readline:

    sCurrentLine = br.readLine();
    System.out.println("Webpage: " + docOne + " Required Doc: " + sCurrentLine);
    sCurrentLine = br.readLine();
    System.out.println("Webpage: " + docTwo + " Required Doc: " + sCurrentLine);

然而,这将使您的代码比现有代码更难阅读。你应该考虑使用Collection(甚至是数组)。

[编辑]根据gparyani的建议,您无需明确调用br.close()。详情请http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html