如何使用Java中的“Startswith”变量查找文件 [英] How to find a file using a variable for 'Startswith' in Java

查看:125
本文介绍了如何使用Java中的“Startswith”变量查找文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据每次迭代从excel表中提取的前8个数字找到一个文件。每当我使用下面的代码时,我都会收到错误消息在封闭范围内定义的局部变量CaseID必须是最终的或有效的最终。我还是新手,所以我不知道怎么解决这个问题,即使这听起来像一个简单的修复。问题发生在代码底部的caseID变量中。

I'm trying to find a file based on the first 8 numbers i pull from an excel sheet for each iteration. Whenever I use the code below I get the error message "Local variable CaseID defined in an enclosing scope must be final or effectively final". I'm still new so i'm not sure how to fix this even though it sounds like a simple fix. The issue is happening at the caseID variable towards the bottom of the code.

  package Chrome;
    //CHROME
    import java.io.File;
    import java.io.FileFilter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;


    public class ValidateInput {

    public static void main(String[] args) throws Throwable {
    // TODO Auto-generated method stub

    // String filePath=System.getProperty("user.dir")+"\\UP.xlsx";
    //File src = new File(filePath);    
    //System.out.println(filePath);
    File src = new File("C:\\Users\\Z246379\\Documents\\TestDataFolder\\ValidateInput.xlsx");          
    FileInputStream fis = new FileInputStream(src);        



    XSSFWorkbook wb = new XSSFWorkbook(fis);        
    XSSFSheet Sheet1 = wb.getSheetAt(0);
    int i1 = 2;
    //Username
    String data0 = Sheet1.getRow(2).getCell(0).getStringCellValue();
    //Password    
    String data01 = Sheet1.getRow(2).getCell(1).getStringCellValue();
    //Case ID
    String caseID = Sheet1.getRow(i1).getCell(2).getStringCellValue();
    // Description
   String Desc = Sheet1.getRow(2).getCell(3).getStringCellValue();
   //Internal Claim File
   String ICF = Sheet1.getRow(1).getCell(4).getStringCellValue();
    String CRPT = Sheet1.getRow(1).getCell(5).getStringCellValue();

    // final String caseID1 = caseID1;




    //Chrome driver code

     System.out.println("Called openBrowser");
     String exePath = System.getProperty("user.dir")+"\\chromedriver.exe";
     System.setProperty("webdriver.chrome.driver", exePath);
     ChromeOptions options = new ChromeOptions();
     options.setExperimentalOption("useAutomationExtension", false);
     options.addArguments("start-maximized");
     options.addArguments("--no-sandbox");
     options.addArguments("--disable-extensions-except");
     options.addArguments("disable-extensions");
     //options.setExperimentalOption("useAutomationExtension", false); 
     WebDriver driver = new ChromeDriver(options);
     driver.get("https://client.abc.com");


     //Logging in       
     driver.findElement(By.xpath("//input[@id='userid']")).sendKeys(data0);
     driver.findElement(By.xpath("//body[@class='no-nav']/div[@id='wrapper']/header[@class='header']/div[@class='container']/div[@class='menu']/div[@class='drop']/div[@class='login-form']/form[@method='POST']/div[2]/input[1]")).sendKeys(data01);
     driver.findElement(By.xpath("//input[@value='Sign In']")).click();
     Thread.sleep(2000);
     //Navigate to Validate Input
     driver.findElement(By.xpath("//span[@class='wpsNavLevel2'][contains(text(),'EZ-Test')]")).click();
     Thread.sleep(3000);
     driver.get("https://ezt.abc.com/Test/inputFiles/selectValidateInput/selectValidateInput.xhtml");

     while (caseID != null)
          {

      caseID = Sheet1.getRow(i1).getCell(2).getStringCellValue();
      //Input Validate Input stuff

      driver.findElement(By.xpath("//input[@id='mainForm:caseId']")).sendKeys(caseID);
    driver.findElement(By.xpath("//input[@id='mainForm:testBedDesc']")).sendKeys(Desc);
      driver.findElement(By.xpath("//select[@id='mainForm:customRptOptions']")).sendKeys(ICF);
     driver.findElement(By.xpath("//span[@id='mainForm:customReportLabel']")).click();




      File dir = new File("C:\\Users\\Z333379\\Documents\\Test beds");
      FilenameFilter filter = new FilenameFilter() {
      public boolean accept (File dir, String name) { 
       return name.startsWith(caseID); //this is where i get the error. its not letting me make case ID a variable. but i really need the starts with stuff
       } 
     }; 
     String[] children = dir.list(filter);
     if (children == null) {
        System.out.println("Either dir does not exist or is not a directory"); 
     } else { 
       for (int i=0; i< children.length; i++) {
       String filename = children[i];
       System.out.println(filename);
            driver.findElement(By.xpath("//input[@id='mainForm:comprehensive']")).sendKeys("C:\\Users\\Z246379\\Documents\\Test beds\\" + filename);
        } 
     } 

     driver.findElement(By.xpath("//button[@id='mainForm:reset']")).click();


     i1=i1+1;
          }

      }
    }


推荐答案

这里:

FilenameFilter filter = new FilenameFilter() {
    public boolean accept (File dir, String name) { 
        return name.startsWith(caseID);
    } 
};

您不能在内部本地类的定义中使用可变变量。这就是你收到错误编译器的原因。

You cannot use a mutable variable in the definition of an inner local class. That's why you receive the error compiler.

创建一个临时变量并在那里分配 caseID 的值,使用这个内部本地类中的变量:

Create a temporary variable and assign the value of caseID there, use this variable in your inner local class:

final String localCaseID = caseID;
FilenameFilter filter = new FilenameFilter() {
    public boolean accept (File dir, String name) { 
        return name.startsWith(localCaseID);
    } 
};

这篇关于如何使用Java中的“Startswith”变量查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆