Convert File[] to String[] in Java Convert File[] to String[] in Java selenium selenium

Convert File[] to String[] in Java


You don't need to convert the whole array. Just call File's getAbsolutePath() method:

driver.findElement(By.id(id1)).sendKeys(listOfFiles[1].getAbsolutePath());

But if you do want to convert the whole array, here is the Java 8 way to do this (simplified by @RemigiusStalder):

String listOfPaths[] = Arrays.stream(listOfFiles).map(File::getAbsolutePath)        .toArray(String[]::new);


I think, you don't need to convert File[] to String[]

Just use your file array this way:

driver.findElement(By.id(id1)).sendKeys(listOfFiles[1].getName());

or, if you would like to send full file path:

driver.findElement(By.id(id1)).sendKeys(listOfFiles[1].getPath());