I have a folder with three files and 2 sub folders. I am only trying to read the path including file names as string from this root folder, however only condition is that i want to exclude other directories located in same folder.
Folder structure:
Folder A
|
|
DirectoryA
fileA.xlsx, fileB.xlsx
What I Expect
All I want is to save path to fileA.xlsx and FileB.xlsx as string in list i.e.
[FolderA/fileA.xlsx, FolderA/fileB.xlsx]
My output:
my output also includes file contents from the sub directory as shown below which i dont want.
[FolderA/DirectoryA/somefile.txt, FolderA/fileA.xlsx, FolderA/fileB.xlsx]
What I tried
public List<String> getAllExcelFilesList() throws IOException {
excelFilesList = new ArrayList<String>();
excelFilesList = Files.walk(Paths.get(path_to_FolderA))
.filter(p -> p.getFileName().toString().startsWith("file"))
.filter(Files::isRegularFile)
.map(Path::toString)
.collect(Collectors.toList());
LOG.info("all files saved to list for extraction");
return excelFilesList;
I also tried following answer here Files.walk(), calculate total size but couldn't make my program to work. maybe i am missing something very insignificant.
EDIT:
from performance point of view it is better if program doesn't traverse through every item inside sub-folder because these sub-directories may include more than 1000 files in the future.