下面列出了怎么用java.nio.file.DirectoryStream.Filter的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
try (DirectoryStream<Path> stream = super.newDirectoryStream(dir, filter)) {
// read complete directory listing
List<Path> contents = new ArrayList<>();
for (Path path : stream) {
contents.add(path);
}
// sort first based only on filename
Collections.sort(contents, (path1, path2) -> path1.getFileName().toString().compareTo(path2.getFileName().toString()));
// sort based on current class seed
Collections.shuffle(contents, new Random(seed));
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
return contents.iterator();
}
@Override
public void close() throws IOException {}
};
}
}
DirectoryStream<Path> newDirectoryStream(final SFTPPath path, Filter<? super Path> filter) throws IOException {
try (Channel channel = channelPool.get()) {
List<LsEntry> entries = channel.listFiles(path.path());
boolean isDirectory = false;
for (Iterator<LsEntry> i = entries.iterator(); i.hasNext(); ) {
LsEntry entry = i.next();
String filename = entry.getFilename();
if (CURRENT_DIR.equals(filename)) {
isDirectory = true;
i.remove();
} else if (PARENT_DIR.equals(filename)) {
i.remove();
}
}
if (!isDirectory) {
// https://github.com/robtimus/sftp-fs/issues/4: don't fail immediately but check the attributes
// Follow links to ensure the directory attribute can be read correctly
SftpATTRS attributes = channel.readAttributes(path.path(), true);
if (!attributes.isDir()) {
throw new NotDirectoryException(path.path());
}
}
return new SFTPPathDirectoryStream(path, entries, filter);
}
}
@Test
public void testFilteredIterator() throws IOException {
final int count = 100;
List<Matcher<? super String>> matchers = new ArrayList<>();
for (int i = 0; i < count; i++) {
if (i % 2 == 1) {
matchers.add(equalTo("file" + i));
}
addFile("/foo/file" + i);
}
List<String> names = new ArrayList<>();
Filter<Path> filter = new PatternFilter("file\\d*[13579]");
try (DirectoryStream<Path> stream = fileSystem.newDirectoryStream(createPath("/foo"), filter)) {
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) {
names.add(iterator.next().getFileName().toString());
}
}
assertThat(names, containsInAnyOrder(matchers));
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
final Filter<? super Path> filter) throws IOException {
final BundleFileSystem fs = (BundleFileSystem) dir.getFileSystem();
final DirectoryStream<Path> stream = origProvider(dir)
.newDirectoryStream(fs.unwrap(dir), new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return filter.accept(fs.wrap(entry));
}
});
return new DirectoryStream<Path>() {
@Override
public void close() throws IOException {
stream.close();
}
@Override
public Iterator<Path> iterator() {
return fs.wrapIterator(stream.iterator());
}
};
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
MCRStoredNode node = MCRFileSystemUtils.resolvePath(mcrPath);
if (node instanceof MCRDirectory) {
return MCRDirectoryStream.getInstance((MCRDirectory) node, mcrPath);
}
throw new NotDirectoryException(dir.toString());
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
MCRFilesystemNode node = resolvePath(mcrPath);
if (node instanceof MCRDirectory) {
return new MCRDirectoryStream((MCRDirectory) node, mcrPath);
}
throw new NotDirectoryException(dir.toString());
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
Filter<? super Path> filter)
throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
Filter<? super Path> filter)
throws IOException
{
JPath jPath = (JPath) dir;
return new JDirectoryStream(jPath, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, final Filter<? super Path> filter) throws IOException {
Filter<Path> wrappedFilter = new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return filter.accept(new FilterPath(entry, fileSystem));
}
};
return new FilterDirectoryStream(delegate.newDirectoryStream(toDelegate(dir), wrappedFilter), fileSystem);
}
public static void deleteAllExtensions(final Path file) throws IOException {
Filter<Path> filter = new ExtensionIgnoringFilter(file);
try (DirectoryStream<Path> ds = newDirectoryStream(file.getParent(),
filter)) {
for (Path p : ds)
deleteRecursively(p);
}
}
private static List<File> listDirectoryChildren(File directory, DirectoryStream.Filter<Path> filter) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory.toPath(), filter)) {
final List<File> result = new ArrayList<>();
for (Path entry : stream) {
result.add(entry.toFile());
}
return result;
} catch (SecurityException | IOException ex) {
throw DeploymentScannerLogger.ROOT_LOGGER.cannotListDirectoryFiles(ex, directory);
}
}
@Override
public DirectoryStream<Path> newDirectoryStream( Path path, Filter<? super Path> filter ) throws IOException {
UnixSshPath unixPath = checkPath( path );
String result = executeForStdout( unixPath, unixPath.getFileSystem().getCommand( "ls" )
+ " -A -1 " + unixPath.toAbsolutePath().quotedString() );
String[] items = result.split( "\n" );
if ( items.length == 1 && items[0].isEmpty() ) {
items = null;
}
return new StandardDirectoryStream( path, items, filter );
}
void walkRelativeFileTree(
Path pathRelativeToProjectRoot,
EnumSet<FileVisitOption> visitOptions,
FileVisitor<Path> fileVisitor,
DirectoryStream.Filter<? super Path> ignoreFilter)
throws IOException {
Path rootPath = getPathForRelativePath(pathRelativeToProjectRoot);
walkFileTreeWithPathMapping(
rootPath, visitOptions, fileVisitor, ignoreFilter, path -> relativize(path).getPath());
}
void walkFileTree(
Path root,
Set<FileVisitOption> options,
FileVisitor<Path> fileVisitor,
DirectoryStream.Filter<? super Path> ignoreFilter)
throws IOException {
root = getPathForRelativePath(root);
new FileTreeWalker(root, options, fileVisitor, ignoreFilter).walk();
}
FileTreeWalker(
Path root,
Set<FileVisitOption> options,
FileVisitor<Path> pathFileVisitor,
DirectoryStream.Filter<? super Path> ignoreFilter) {
this.followLinks = options.contains(FileVisitOption.FOLLOW_LINKS);
this.visitor = pathFileVisitor;
this.root = root;
this.state = new ArrayDeque<>();
this.ignoreFilter = ignoreFilter;
}
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException
{
return new ZipDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException
{
return toZipPath(path).newDirectoryStream(filter);
}
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException
{
return new ZipDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException
{
return toZipPath(path).newDirectoryStream(filter);
}
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException
{
return new ZipDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException
{
return toZipPath(path).newDirectoryStream(filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
return null;
}
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException
{
return new ZipDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException
{
return toZipPath(path).newDirectoryStream(filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
Filter<? super Path> filter) throws IOException {
return null;
}
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException
{
return new ZipDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException
{
return toZipPath(path).newDirectoryStream(filter);
}
final DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException {
return new JrtDirectoryStream(this, filter);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path path, Filter<? super Path> filter) throws IOException {
return toJrtPath(path).newDirectoryStream(filter);
}
final DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter)
throws IOException {
return new JrtDirectoryStream(this, filter);
}