java.nio.file.Files.newDirectoryStream()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中java.nio.file.Files.newDirectoryStream()方法的一些代码示例,展示了Files.newDirectoryStream()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.newDirectoryStream()方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:newDirectoryStream

Files.newDirectoryStream介绍

暂无

代码示例

代码示例来源:origin: jenkinsci/jenkins

private List<IOException> tryRemoveDirectoryContents(@Nonnull Path path) {
  Path normalized = path.normalize();
  List<IOException> accumulatedErrors = new ArrayList<>();
  if (!Files.isDirectory(normalized)) return accumulatedErrors;
  try (DirectoryStream<Path> children = Files.newDirectoryStream(normalized)) {
    for (Path child : children) {
      accumulatedErrors.addAll(tryRemoveRecursive(child));
    }
  } catch (IOException e) {
    accumulatedErrors.add(e);
  }
  return accumulatedErrors;
}

代码示例来源:origin: SonarSource/sonarqube

private static DirectoryStream<Path> list(Path dir) throws IOException {
  return Files.newDirectoryStream(dir, entry -> !DirectoryLock.LOCK_FILE_NAME.equals(entry.getFileName().toString()));
 }
}

代码示例来源:origin: apache/storm

private void forEachTopologyDistDir(ConsumePathAndId consumer) throws IOException {
  Path stormCodeRoot = Paths.get(ConfigUtils.supervisorStormDistRoot(conf));
  if (Files.exists(stormCodeRoot) && Files.isDirectory(stormCodeRoot)) {
    try (DirectoryStream<Path> children = Files.newDirectoryStream(stormCodeRoot)) {
      for (Path child : children) {
        if (Files.isDirectory(child)) {
          String topologyId = child.getFileName().toString();
          consumer.accept(child, topologyId);
        }
      }
    }
  }
}

代码示例来源:origin: twosigma/beakerx

@NotNull
private List<String> listDirectory(Path path) throws IOException {
 List<String> result = new ArrayList<>();
 Files.newDirectoryStream(path, filter)
     .forEach(x -> result.add(x.toString()));
 return result;
}

代码示例来源:origin: apache/ignite

if (Files.isDirectory(path)) {
  try {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
      for (Path innerPath : stream) {
        boolean res = delete(innerPath);
  try {
    new JarFile(path.toString(), false).close();

代码示例来源:origin: springside/springside4

/**
 * 复制目录
 */
public static void copyDir(@NotNull Path from, @NotNull Path to) throws IOException {
  Validate.isTrue(isDirExists(from), "%s is not exist or not a dir", from);
  Validate.notNull(to);
  makesureDirExists(to);
  try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(from)) {
    for (Path path : dirStream) {
      copy(path, to.resolve(path.getFileName()));
    }
  }
}

代码示例来源:origin: neo4j/neo4j

private void copyRecursively( Path source, Path target ) throws IOException
{
  try ( DirectoryStream<Path> directoryStream = Files.newDirectoryStream( source ) )
  {
    for ( Path sourcePath : directoryStream )
    {
      Path targetPath = target.resolve( sourcePath.getFileName() );
      if ( Files.isDirectory( sourcePath ) )
      {
        Files.createDirectories( targetPath );
        copyRecursively( sourcePath, targetPath );
      }
      else
      {
        Files.copy( sourcePath, targetPath,
            REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES );
      }
    }
  }
}

代码示例来源:origin: apache/flink

static void processConfigOptions(String rootDir, String module, String packageName, String pathPrefix, ThrowingConsumer<Class<?>, IOException> classConsumer) throws IOException, ClassNotFoundException {
  Path configDir = Paths.get(rootDir, module, pathPrefix, packageName.replaceAll("\\.", "/"));
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
    for (Path entry : stream) {
      String fileName = entry.getFileName().toString();
      Matcher matcher = CLASS_NAME_PATTERN.matcher(fileName);
      if (matcher.matches()) {
        final String className = packageName + '.' + matcher.group(CLASS_NAME_GROUP);
        if (!EXCLUSIONS.contains(className)) {
          Class<?> optionsClass = Class.forName(className);
          classConsumer.accept(optionsClass);
        }
      }
    }
  }
}

代码示例来源:origin: OryxProject/oryx

Preconditions.checkArgument(Files.isDirectory(dir), "%s is not a directory", dir);
 List<Path> newPaths = new ArrayList<>();
 for (Path existingPath : paths) {
  if (Files.isDirectory(existingPath)) {
   try (DirectoryStream<Path> stream = Files.newDirectoryStream(existingPath, globLevel)) {
    for (Path path : stream) {
     if (!path.getFileName().toString().startsWith(".")) {
      newPaths.add(path);

代码示例来源:origin: twosigma/beakerx

@NotNull
private List<String> listParentDirectory(Path path) throws IOException {
 List<String> result = new ArrayList<>();
 Path parent = path.getParent();
 Files.newDirectoryStream(parent, p -> {
  if (p.toString().startsWith(path.toString())) {
   if (p.toFile().isFile()) {
    return p.toString().endsWith(".jar");
   }
   return true;
  }
  return false;
 }).forEach(x -> result.add(x.toString()));
 return result;
}

代码示例来源:origin: google/error-prone

private static void findExamples(
  List<Path> examples, Path dir, DirectoryStream.Filter<Path> filter) throws IOException {
 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
  for (Path entry : stream) {
   if (Files.isDirectory(entry)) {
    findExamples(examples, entry, filter);
   } else {
    examples.add(entry);
   }
  }
 }
}

代码示例来源:origin: apache/ignite

/**
 * Parse all IGFS log files in specified log directory.
 *
 * @param logDir Folder were log files located.
 * @return List of line with aggregated information by files.
 */
private List<VisorIgfsProfilerEntry> parse(Path logDir, String igfsName) throws IOException {
  List<VisorIgfsProfilerEntry> parsedFiles = new ArrayList<>(512);
  try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(logDir)) {
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:igfs-log-" + igfsName + "-*.csv");
    for (Path p : dirStream) {
      if (matcher.matches(p.getFileName())) {
        try {
          parsedFiles.addAll(parseFile(p));
        }
        catch (NoSuchFileException ignored) {
          // Files was deleted, skip it.
        }
        catch (Exception e) {
          ignite.log().warning("Failed to parse IGFS profiler log file: " + p, e);
        }
      }
    }
  }
  return parsedFiles;
}

代码示例来源:origin: google/jimfs

if (Files.isDirectory(path)) {
 try (DirectoryStream<Path> files = Files.newDirectoryStream(path)) {
  for (Path file : files) {
   builder.append(file.getFileName()).append('\n');

代码示例来源:origin: bwssytems/ha-bridge

public List<String> getBackups() {
  List<String> theFilenames = new ArrayList<String>();
  Path dir = repositoryPath.getParent();
  try (DirectoryStream<Path> stream =
     Files.newDirectoryStream(dir, "*.{"+ fileExtension.substring(1) + "}")) {
    for (Path entry: stream) {
      theFilenames.add(entry.getFileName().toString());
    }
  } catch (IOException x) {
    // IOException can never be thrown by the iteration.
    // In this snippet, it can // only be thrown by newDirectoryStream.
    log.warn("Issue getting directory listing for backups in directory: " + x.getMessage());
  }
  return theFilenames;
}

代码示例来源:origin: wildfly/wildfly

if (levels == 0) {
  try {
    stream = Files.newDirectoryStream(root, "*.xml");
    iterator = stream.iterator();
  } catch (IOException e) {
} else {
  try {
    stream = Files.newDirectoryStream(root, entry -> {
      final String fileName = entry.getFileName().toString();
      return fileName.length() == 1 && !fileName.equals(".") && Files.isDirectory(entry);
    });
    iterator = stream.iterator();

代码示例来源:origin: allure-framework/allure2

private List<URL> jarsInDirectory(final Path directory) {
  final DirectoryStream.Filter<Path> pathFilter = entry ->
      Files.isRegularFile(entry) && entry.toString().endsWith(".jar");
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, pathFilter)) {
    return StreamSupport.stream(stream.spliterator(), false)
        .filter(Files::isRegularFile)
        .map(this::toUrlSafe)
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());
  } catch (IOException e) {
    LOGGER.error("Could not load plugin: {}", e);
    return Collections.emptyList();
  }
}

代码示例来源:origin: google/guava

/**
 * Insecure recursive delete for file systems that don't support {@code SecureDirectoryStream}.
 * Returns a collection of exceptions that occurred or null if no exceptions were thrown.
 */
private static @Nullable Collection<IOException> deleteRecursivelyInsecure(Path path) {
 Collection<IOException> exceptions = null;
 try {
  if (Files.isDirectory(path, NOFOLLOW_LINKS)) {
   try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
    exceptions = deleteDirectoryContentsInsecure(stream);
   }
  }
  // If exceptions is not null, something went wrong trying to delete the contents of the
  // directory, so we shouldn't try to delete the directory as it will probably fail.
  if (exceptions == null) {
   Files.delete(path);
  }
  return exceptions;
 } catch (IOException e) {
  return addException(exceptions, e);
 }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@AfterClass
public static void afterClass() throws Exception {
  long size = 0;
  try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
    for (final Path path : directoryStream) {
      if (size == 0) {
        size = Files.size(path);
      } else {
        final long fileSize = Files.size(path);
        assertTrue("Expected size: " + size + " Size of " + path.getFileName() + ": " + fileSize,
            size == fileSize);
      }
      Files.delete(path);
    }
    Files.delete(Paths.get(DIR));
  }
}

代码示例来源:origin: wildfly/wildfly

@Override
public List<Resource> list() {
  final List<Resource> resources = new ArrayList<>();
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(file)) {
    for (Path child : stream) {
      resources.add(new PathResource(child, manager, path + file.getFileSystem().getSeparator() + child.getFileName().toString()));
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return resources;
}

代码示例来源:origin: igniterealtime/Openfire

if ( !Files.isDirectory( pluginsDirectory ) || !Files.isReadable( pluginsDirectory ) )
try ( final DirectoryStream<Path> ds = Files.newDirectoryStream( pluginsDirectory, new DirectoryStream.Filter<Path>()
    final String fileName = jarFile.getFileName().toString();
    final String canonicalPluginName = fileName.substring( 0, fileName.length() - 4 ).toLowerCase(); // strip extension.
try ( final DirectoryStream<Path> ds = Files.newDirectoryStream( pluginsDirectory, new DirectoryStream.Filter<Path>()
try ( final DirectoryStream<Path> ds = Files.newDirectoryStream( pluginsDirectory, new DirectoryStream.Filter<Path>()
        if ( Files.exists( devPluginPath ) && Files.isDirectory( devPluginPath ) )

相关文章

微信公众号

最新文章

更多