@Test public void testGetAnyNonHiddenFile() throws IOException { final String file1 = "test1"; FileSystem localFs = FileSystem.getLocal(new Configuration()); Path baseDir = new Path(FILE_UTILS_TEST_DIR, "anyFileDir"); try { if (localFs.exists(baseDir)) { localFs.delete(baseDir, true); } localFs.mkdirs(baseDir); Path emptySubDir = new Path(baseDir, "emptySubDir"); localFs.mkdirs(emptySubDir); Path hiddenDir = new Path(baseDir, "_hidden"); localFs.mkdirs(hiddenDir); localFs.create(new Path(hiddenDir, file1)); Path dataDir = new Path(baseDir, "dataDir"); localFs.mkdirs(dataDir); File dataFile = new File(dataDir.toString(), file1); localFs.create(new Path(dataDir, file1)); FileStatus file = FileListUtils.getAnyNonHiddenFile(localFs, baseDir); Assert.assertEquals(file.getPath().toString(), dataFile.toURI().toString()); } finally { localFs.delete(baseDir, true); } }
public static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path) throws IOException { HiddenFilter hiddenFilter = new HiddenFilter(); FileStatus root = fs.getFileStatus(path); if (!root.isDirectory()) { return hiddenFilter.accept(path) ? root : null; } Stack<FileStatus> folders = new Stack<>(); folders.push(root); while (!folders.empty()) { FileStatus curFolder = folders.pop(); try { for (FileStatus status : fs.listStatus(curFolder.getPath(), hiddenFilter)) { if (status.isDirectory()) { folders.push(status); } else { return status; } } } catch (FileNotFoundException exc) { } } return null; }
FileListUtils { public static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path) throws IOException { HiddenFilter hiddenFilter = new HiddenFilter(); FileStatus root = fs.getFileStatus(path); if (!root.isDirectory()) { return hiddenFilter.accept(path) ? root : null; } Stack<FileStatus> folders = new Stack<>(); folders.push(root); while (!folders.empty()) { FileStatus curFolder = folders.pop(); try { for (FileStatus status : fs.listStatus(curFolder.getPath(), hiddenFilter)) { if (status.isDirectory()) { folders.push(status); } else { return status; } } } catch (FileNotFoundException exc) { } } return null; } }
FileListUtils { public static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path) throws IOException { HiddenFilter hiddenFilter = new HiddenFilter(); FileStatus root = fs.getFileStatus(path); if (!root.isDirectory()) { return hiddenFilter.accept(path) ? root : null; } Stack<FileStatus> folders = new Stack<>(); folders.push(root); while (!folders.empty()) { FileStatus curFolder = folders.pop(); try { for (FileStatus status : fs.listStatus(curFolder.getPath(), hiddenFilter)) { if (status.isDirectory()) { folders.push(status); } else { return status; } } } catch (FileNotFoundException exc) { } } return null; }  }
FileListUtils { public static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path) throws IOException { HiddenFilter hiddenFilter = new HiddenFilter(); FileStatus root = fs.getFileStatus(path); if (!root.isDirectory()) { return hiddenFilter.accept(path) ? root : null; } Stack<FileStatus> folders = new Stack<>(); folders.push(root); while (!folders.empty()) { FileStatus curFolder = folders.pop(); try { for (FileStatus status : fs.listStatus(curFolder.getPath(), hiddenFilter)) { if (status.isDirectory()) { folders.push(status); } else { return status; } } } catch (FileNotFoundException exc) { } } return null; }  static List<FileStatus> listFilesRecursively(FileSystem fs, Path path); static List<FileStatus> listFilesRecursively(FileSystem fs, Iterable<Path> paths); static List<FileStatus> listFilesToCopyAtPath(FileSystem fs, Path path, PathFilter fileFilter,
      boolean includeEmptyDirectories); static List<FileStatus> listFilesToCopyAtPath(FileSystem fs, Path path, PathFilter fileFilter,
      boolean applyFilterToDirectories, boolean includeEmptyDirectories); static List<FileStatus> listFilesRecursively(FileSystem fs, Path path, PathFilter fileFilter); static List<FileStatus> listFilesRecursively(FileSystem fs, Path path, PathFilter fileFilter,
      boolean applyFilterToDirectories); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Path path); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Iterable<Path> paths); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Path path, PathFilter fileFilter); static List<FileStatus> listPathsRecursively(FileSystem fs, Path path, PathFilter fileFilter); static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path); }
FileListUtils { public static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path) throws IOException { HiddenFilter hiddenFilter = new HiddenFilter(); FileStatus root = fs.getFileStatus(path); if (!root.isDirectory()) { return hiddenFilter.accept(path) ? root : null; } Stack<FileStatus> folders = new Stack<>(); folders.push(root); while (!folders.empty()) { FileStatus curFolder = folders.pop(); try { for (FileStatus status : fs.listStatus(curFolder.getPath(), hiddenFilter)) { if (status.isDirectory()) { folders.push(status); } else { return status; } } } catch (FileNotFoundException exc) { } } return null; }  static List<FileStatus> listFilesRecursively(FileSystem fs, Path path); static List<FileStatus> listFilesRecursively(FileSystem fs, Iterable<Path> paths); static List<FileStatus> listFilesToCopyAtPath(FileSystem fs, Path path, PathFilter fileFilter,
      boolean includeEmptyDirectories); static List<FileStatus> listFilesToCopyAtPath(FileSystem fs, Path path, PathFilter fileFilter,
      boolean applyFilterToDirectories, boolean includeEmptyDirectories); static List<FileStatus> listFilesRecursively(FileSystem fs, Path path, PathFilter fileFilter); static List<FileStatus> listFilesRecursively(FileSystem fs, Path path, PathFilter fileFilter,
      boolean applyFilterToDirectories); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Path path); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Iterable<Path> paths); static List<FileStatus> listMostNestedPathRecursively(FileSystem fs, Path path, PathFilter fileFilter); static List<FileStatus> listPathsRecursively(FileSystem fs, Path path, PathFilter fileFilter); static FileStatus getAnyNonHiddenFile(FileSystem fs, Path path); static final Comparator<FileStatus> LATEST_MOD_TIME_ORDER; static final PathFilter NO_OP_PATH_FILTER; }