下面列出了java.util.logging.ErrorManager#java.nio.file.DirectoryStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return the complete list of files in a directory as strings.<p/>
*
* This is better than File#listDir because it does not ignore IOExceptions.
*
* @param dir The directory to list.
* @param filter If non-null, the filter to use when listing
* this directory.
* @return The list of files in the directory.
*
* @throws IOException On I/O error
*/
public static List<String> listDirectory(File dir, FilenameFilter filter)
throws IOException {
ArrayList<String> list = new ArrayList<String> ();
try (DirectoryStream<Path> stream =
Files.newDirectoryStream(dir.toPath())) {
for (Path entry: stream) {
String fileName = entry.getFileName().toString();
if ((filter == null) || filter.accept(dir, fileName)) {
list.add(fileName);
}
}
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
return list;
}
public void iterateOverMFileCollection(Visitor visit) throws IOException {
if (debug)
System.out.printf(" iterateOverMFileCollection %s ", collectionDir);
int count = 0;
try (DirectoryStream<Path> ds = Files.newDirectoryStream(collectionDir, new MyStreamFilter())) {
for (Path p : ds) {
try {
BasicFileAttributes attr = Files.readAttributes(p, BasicFileAttributes.class);
if (!attr.isDirectory())
visit.consume(new MFileOS7(p));
if (debug)
System.out.printf("%d ", count++);
} catch (IOException ioe) {
// catch error and skip file
logger.error("Failed to read attributes from file found in Files.newDirectoryStream ", ioe);
}
}
}
if (debug)
System.out.printf("%d%n", count);
}
@Test
public void changeUserDirTest() throws ScriptException, IOException {
System.setProperty("nashorn.persistent.code.cache", codeCache);
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
final Path codeCachePath = getCodeCachePath(false);
final String newUserDir = "build/newUserDir";
// Now changing current working directory
System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
try {
// Check that a new compiled script is stored in existing code cache
e.eval(code1);
final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
checkCompiledScripts(stream, 1);
// Setting to default current working dir
} finally {
System.setProperty("user.dir", oldUserDir);
}
}
static void checkForFailedPluginRemovals(final Path pluginsDirectory) throws IOException {
/*
* Check for the existence of a marker file that indicates any plugins are in a garbage state from a failed attempt to remove the
* plugin.
*/
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pluginsDirectory, ".removing-*")) {
final Iterator<Path> iterator = stream.iterator();
if (iterator.hasNext()) {
final Path removing = iterator.next();
final String fileName = removing.getFileName().toString();
final String name = fileName.substring(1 + fileName.indexOf("-"));
final String message = String.format(
Locale.ROOT,
"found file [%s] from a failed attempt to remove the plugin [%s]; execute [elasticsearch-plugin remove %2$s]",
removing,
name);
throw new IllegalStateException(message);
}
}
}
private void portApplication(final String appId) {
logger.info("Upgrading application " + appId);
/* Process every table */
try {
DirectoryStream<Path> stream = Files.newDirectoryStream(FileSystems.getDefault().getPath(BSql.BSQL_BASE_FOLDER + appId + BSql.SEPERATOR + BSql.DATABASE_FOLDER_NAME));
stream.iterator().forEachRemaining(path -> {
if (!path.toFile().isDirectory()) {
return;
}
processTable(appId, path.getFileName().toString());
});
} catch (IOException ex) {
java.util.logging.Logger.getLogger(Version2to3.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void deleteFileAndEmptyDirectories(Path file) throws IOException {
if (Files.isRegularFile(file)) {
Files.delete(file);
}
if (Files.isDirectory(file)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(file)) {
for (@SuppressWarnings("unused")
Path entry : directoryStream) {
return;
}
Files.delete(file);
}
}
Path parent = file.getParent();
if (parent != null && parent.getNameCount() > 0) {
deleteFileAndEmptyDirectories(parent);
}
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
throws IOException
{
super(dir, watcher);
this.fileKey = fileKey;
this.valid = true;
this.tickCount = 0;
this.entries = new HashMap<Path,CacheEntry>();
// get the initial entries in the directory
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) {
// don't follow links
long lastModified =
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
}
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
}
@BeforeClass
public static void beforeClass() throws Exception {
if (Files.exists(Paths.get(DIR))) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
for (final Path path : directoryStream) {
Files.delete(path);
}
Files.delete(Paths.get(DIR));
}
}
File dir = new File(DIR);
if (!dir.exists()) {
Files.createDirectory(new File(DIR).toPath());
}
Path target = Paths.get(DIR, TARGET + System.currentTimeMillis());
Files.copy(Paths.get(SOURCE, FILENAME), target, StandardCopyOption.COPY_ATTRIBUTES);
}
private static List<Path> collectIndexSubPaths(NodePath[] nodePaths, Predicate<Path> subPathPredicate) throws IOException {
List<Path> indexSubPaths = new ArrayList<>();
for (NodePath nodePath : nodePaths) {
Path indicesPath = nodePath.indicesPath;
if (Files.isDirectory(indicesPath)) {
try (DirectoryStream<Path> indexStream = Files.newDirectoryStream(indicesPath)) {
for (Path indexPath : indexStream) {
if (Files.isDirectory(indexPath)) {
try (Stream<Path> shardStream = Files.list(indexPath)) {
shardStream.filter(subPathPredicate)
.map(Path::toAbsolutePath)
.forEach(indexSubPaths::add);
}
}
}
}
}
}
return indexSubPaths;
}
@NotNull
public static List<Path> findAllPluginFolders(@NotNull final Path pluginPath) throws IOException {
checkNotNull(pluginPath, "provided extension folder path CAN NOT be null");
final ImmutableList.Builder<Path> builder = ImmutableList.builder();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(pluginPath)) {
for (final Path path : stream) {
if (PluginUtil.isValidPluginFolder(path, true)) {
log.trace("Found extension folder {}", path.toString());
builder.add(path);
}
}
}
return builder.build();
}
private void start(String loc) {
Path base = Paths.get(loc);
try {
DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(base);
for (Path p : newDirectoryStream) {
DirectoryStream<Path> a = Files.newDirectoryStream(p);
int c = 0;
for (Path b : a) {
c++;
}
if (c == 1) {
org.apache.commons.io.FileUtils.deleteDirectory(p.toFile());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void process() {
System.out.println("# jar_in_dir: " + root);
if (!Files.exists(root)) {
return;
}
try (DirectoryStream<Path> ds
= Files.newDirectoryStream(root, "*.jar")) {
for (Path p : ds) {
new ClassPathJarEntry(p, executor).process();
if (isFinished()) {
return;
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
protected List<T> getAll(Path directory, String glob) throws IOException {
List<T> objects = new ArrayList<>();
try (DirectoryStream<Path> directoryStream = newDirectoryStream(directory, glob)) {
for (Path path : directoryStream) {
String id = getComponentId(path);
Optional<T> e = tryGet(directory.resolve(format("%s/%s.json", id, id)));
if (e.isPresent()) {
objects.add(e.get());
} else {
logger.error(format("%s %s cannot be loaded, your repository may be corrupted",
type.getClass().getSimpleName(), id));
}
}
}
return objects;
}
static void verifyInvokerBytecodeGenerator() throws Exception {
int count = 0;
int mcount = 0;
try (DirectoryStream<Path> ds = newDirectoryStream(new File(".").toPath(),
// filter in lambda proxy classes
"A$I$$Lambda$?.class")) {
for (Path p : ds) {
System.out.println(p.toFile());
ClassFile cf = ClassFile.read(p.toFile());
// Check those methods implementing Supplier.get
mcount += checkMethod(cf, "get");
count++;
}
}
if (count < 3) {
throw new RuntimeException("unexpected number of files, "
+ "expected atleast 3 files, but got only " + count);
}
if (mcount < 3) {
throw new RuntimeException("unexpected number of methods, "
+ "expected atleast 3 methods, but got only " + mcount);
}
}
@Override
public void process() {
System.out.println("# jar_in_dir: " + root);
if (!Files.exists(root)) {
return;
}
try (DirectoryStream<Path> ds
= Files.newDirectoryStream(root, "*.jar")) {
for (Path p : ds) {
new ClassPathJarEntry(p, executor).process();
if (isFinished()) {
return;
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private List<Path> getClassPaths(String cpaths) {
if (cpaths.isEmpty()) {
return Collections.emptyList();
}
List<Path> paths = new ArrayList<>();
for (String p : cpaths.split(File.pathSeparator)) {
if (p.length() > 0) {
// wildcard to parse all JAR files e.g. -classpath dir/*
int i = p.lastIndexOf(".*");
if (i > 0) {
Path dir = Paths.get(p.substring(0, i));
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
for (Path entry : stream) {
paths.add(entry);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
paths.add(Paths.get(p));
}
}
}
return paths;
}
/**
* Return the list of assets found in a repository
*/
public List<Asset> findAssetInPath(T component, AssetType type, Path directory) throws IOException {
List<Asset> objects = new ArrayList<>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
for (Path path : directoryStream) {
objects.add(new Asset()
.setName(path.getFileName().toString())
.setType(type)
.setScope(AssetScope.forComponent(component))
.setComponentId(component.getId())
.setId(UUID.randomUUID().toString()));
}
}
return objects;
}
private static List<SourcePath> doScan(File root, File dir) {
List<SourcePath> sourcePaths = new ArrayList<>();
DirectoryStream<Path> dirStream = null;
try {
dirStream = Files.newDirectoryStream(dir.toPath());
Iterator<Path> it = dirStream.iterator();
while (it.hasNext()) {
Path next = it.next();
if (Files.isDirectory(next)) {
sourcePaths.addAll(doScan(root, next.toFile()));
} else {
sourcePaths.add(new SourcePath(root, next.toFile()));
}
}
} catch (IOException ex) {
if (dirStream != null) {
try {
dirStream.close();
} catch (IOException e) { }
}
}
return sort(sourcePaths);
}
private static String findRpmCACert(Path docrootPath) {
String candidateRpmCA = StringUtils.EMPTY;
try (DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(docrootPath, CA_CRT_RPM_NAME + GLOB_NOARCH_RPM)) {
for (Path rpmFile : directoryStream) {
logger.debug("Found CA RPM file: " + candidateRpmCA);
if (rpmFile.toString().compareTo(candidateRpmCA) > 0) {
candidateRpmCA = rpmFile.toString();
}
}
return candidateRpmCA;
}
catch (IOException | DirectoryIteratorException ex) {
logger.warn("Cannot scan docroot " + docrootPath +
" for CA RPM certificate. Exception: " + ex);
}
return candidateRpmCA;
}
/**
* @param withDiscoveredStatus
* whether the tuples generated should contain a Status field
* with DISCOVERED as value and be emitted on the status stream
* @param directory
* containing the seed files
* @param filter
* to apply on the file names
* @since 1.13
**/
public FileSpout(String dir, String filter, boolean withDiscoveredStatus) {
this.withDiscoveredStatus = withDiscoveredStatus;
Path pdir = Paths.get(dir);
_inputFiles = new LinkedList<>();
LOG.info("Reading directory: {} (filter: {})", pdir, filter);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pdir,
filter)) {
for (Path entry : stream) {
String inputFile = entry.toAbsolutePath().toString();
_inputFiles.add(inputFile);
LOG.info("Input : {}", inputFile);
}
} catch (IOException ioe) {
LOG.error("IOException: %s%n", ioe);
}
}
private static void translateOneLanguage(Path assetsDir, String language, final Collection<String> filesToTranslate)
throws IOException {
Path targetHtmlDir = assetsDir.resolve("html-" + language);
Files.createDirectories(targetHtmlDir);
Path englishHtmlDir = assetsDir.resolve("html-en");
String translationTextTranslated = StringsResourceTranslator.translateString("Translated by Google Translate.",
language);
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
String name = entry.getFileName().toString();
return name.endsWith(".html") && (filesToTranslate.isEmpty() || filesToTranslate.contains(name));
}
};
try (DirectoryStream<Path> files = Files.newDirectoryStream(englishHtmlDir, filter)) {
for (Path sourceFile : files) {
translateOneFile(language, targetHtmlDir, sourceFile, translationTextTranslated);
}
}
}
@Override
public List<Node> getChildren() {
if (!isDirectory())
throw new IllegalArgumentException("not a directory: " + getNameString());
if (children == null) {
List<Node> list = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path p : stream) {
p = explodedModulesDir.relativize(p);
String pName = MODULES + nativeSlashToFrontSlash(p.toString());
Node node = findNode(pName);
if (node != null) { // findNode may choose to hide certain files!
list.add(node);
}
}
} catch (IOException x) {
return null;
}
children = list;
}
return children;
}
/**
* @return log segment files sorted based on their index.
*/
public List<LogPathAndIndex> getLogSegmentFiles() throws IOException {
List<LogPathAndIndex> list = new ArrayList<>();
try (DirectoryStream<Path> stream =
Files.newDirectoryStream(getCurrentDir().toPath())) {
for (Path path : stream) {
for (Pattern pattern : Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX)) {
Matcher matcher = pattern.matcher(path.getFileName().toString());
if (matcher.matches()) {
if (pattern == OPEN_SEGMENT_REGEX && Files.size(path) == 0L) {
Files.delete(path);
LOG.info("Delete zero size file " + path);
break;
}
final long startIndex = Long.parseLong(matcher.group(1));
final long endIndex = matcher.groupCount() == 2 ?
Long.parseLong(matcher.group(2)) : INVALID_LOG_INDEX;
list.add(new LogPathAndIndex(path, startIndex, endIndex));
break;
}
}
}
}
list.sort(Comparator.comparingLong(o -> o.startIndex));
return list;
}
/**
* Scans the hunspell directory and loads all found dictionaries
*/
private void scanAndLoadDictionaries() throws IOException {
if (Files.isDirectory(hunspellDir)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(hunspellDir)) {
for (Path file : stream) {
if (Files.isDirectory(file)) {
try (DirectoryStream<Path> inner = Files.newDirectoryStream(hunspellDir.resolve(file), "*.dic")) {
if (inner.iterator().hasNext()) { // just making sure it's indeed a dictionary dir
try {
dictionaries.getUnchecked(file.getFileName().toString());
} catch (UncheckedExecutionException e) {
// The cache loader throws unchecked exception (see #loadDictionary()),
// here we simply report the exception and continue loading the dictionaries
logger.error("exception while loading dictionary {}", file.getFileName(), e);
}
}
}
}
}
}
}
}
public int delete(User user, Function<Path, Boolean> filter) {
log.debug("Removing all reporting data for {}", user.email);
Path reportingFolderPath = getUserReportingFolderPath(user);
int removedFilesCounter = 0;
try {
if (Files.exists(reportingFolderPath)) {
try (DirectoryStream<Path> reportingFolder = Files.newDirectoryStream(reportingFolderPath, "*")) {
for (Path reportingFile : reportingFolder) {
if (filter.apply(reportingFile)) {
log.trace("Removing {}", reportingFile);
FileUtils.deleteQuietly(reportingFile);
removedFilesCounter++;
}
}
}
}
} catch (Exception e) {
log.error("Error removing file : {}.", reportingFolderPath);
}
return removedFilesCounter;
}
/**
* Add path to the stack if path is directory, otherwise delete it.
*
* @param paths Stack of paths.
* @param ds Stream of paths.
*/
private static void appendOrRemove(LinkedList<Path> paths, DirectoryStream<Path> ds) throws IOException {
for (Path p : ds) {
if (Files.isDirectory(p))
paths.add(p);
else
Files.delete(p);
}
}
/**
* @param dir directory to list
* @param glob glob pattern for files that should be returned, which can span subdirectories
* as in patterns like {@code * /*}
* @return {@link Path}s, including both files and directories, matching the glob pattern.
* No path containing an element whose name starts with "." is returned.
* Returned paths are also ordered lexicographically.
* @throws IOException if an error occurs while accessing the file system
*/
public static List<Path> listFiles(Path dir, String glob) throws IOException {
Preconditions.checkArgument(Files.isDirectory(dir), "%s is not a directory", dir);
List<String> globLevels;
if (glob == null || glob.isEmpty()) {
globLevels = Collections.singletonList("*");
} else {
globLevels = Arrays.asList(glob.split("/"));
}
Preconditions.checkState(!globLevels.isEmpty());
List<Path> paths = new ArrayList<>();
paths.add(dir);
for (String globLevel : globLevels) {
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);
}
}
}
}
}
paths = newPaths;
}
Collections.sort(paths);
return paths;
}
private void verifySymLinks(String bindir) throws IOException {
File binDir = new File(bindir);
System.err.println("verifying links in: " + bindir);
File isaDir = new File(binDir, getArch()).getAbsoluteFile();
if (!isaDir.exists()) {
throw new RuntimeException("dir: " + isaDir + " does not exist");
}
try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {
for (Path p : ds) {
if (symlinkExcludes.matcher(p.toString()).matches() ||
Files.isDirectory(p, NOFOLLOW_LINKS)) {
continue;
}
Path link = new File(isaDir, p.getFileName().toString()).toPath();
if (Files.isSymbolicLink(link)) {
Path target = Files.readSymbolicLink(link);
if (target.startsWith("..") && p.endsWith(target.getFileName())) {
// System.out.println(target + " OK");
continue;
}
System.err.println("target:" + target);
System.err.println("file:" + p);
}
throw new RuntimeException("could not find link to " + p);
}
}
}
/** If the file is a directory, list its contents. */
public Iterable<DocFile> list() throws IOException {
List<DocFile> files = new ArrayList<DocFile>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) {
for (Path f: ds) {
files.add(new StandardDocFile(f));
}
}
return files;
}