使用nio.file.DirectoryStream递归列出目录中的所有文件;

时间:2021-09-18 07:08:51

I want to list all the FILES within the specified directory and subdirectories within that directory. No directories should be listed.

我想列出指定目录中的所有文件,以及该目录中的子目录。不应该列出目录。

My current code is below. It does not work properly as it only lists the files and directories within the specified directory.

我的当前代码在下面。它不能正常工作,因为它只列出指定目录中的文件和目录。

How can I fix this?

我该怎么解决这个问题呢?

final List<Path> files = new ArrayList<>();

Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
try
{
  DirectoryStream<Path> stream;
  stream = Files.newDirectoryStream(path);
  for (Path entry : stream)
  {
    files.add(entry);
  }
  stream.close();
}
catch (IOException e)
{
  e.printStackTrace();
}

for (Path entry: files)
{
  System.out.println(entry.toString());
}

Regards.

的问候。

8 个解决方案

#1


35  

Java 8 provides a nice way for that:

Java 8提供了一种很好的方式:

Files.walk(path)

This method returns Stream<Path>.

该方法返回流 <路径> 。

#2


29  

Make a method which will call itself if a next element is directory

创建一个方法,如果下一个元素是目录,它将调用自己

void listFiles(Path path) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                listFiles(entry);
            }
            files.add(entry);
        }
    }
}

#3


24  

Check FileVisitor, very neat.

检查中FileVisitor非常整洁。

 Path path= Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
 final List<Path> files=new ArrayList<>();
 try {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
     @Override
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
          if(!attrs.isDirectory()){
               files.add(file);
          }
          return FileVisitResult.CONTINUE;
      }
     });
 } catch (IOException e) {
      e.printStackTrace();
 }

#4


5  

If you want to avoid having the function calling itself recursively and having a file list that is a member variable, you can use a stack:

如果您想避免函数递归地调用自身,并且有一个作为成员变量的文件列表,您可以使用堆栈:

private List<Path> listFiles(Path path) throws IOException {
    Deque<Path> stack = new ArrayDeque<Path>();
    final List<Path> files = new LinkedList<>();

    stack.push(path);

    while (!stack.isEmpty()) {
        DirectoryStream<Path> stream = Files.newDirectoryStream(stack.pop());
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                stack.push(entry);
            }
            else {
                files.add(entry);
            }
        }
        stream.close();
    }

    return files;
}

#5


2  

Using Rx Java, the requirement can be solved in a number of ways while sticking to usage of DirectoryStream from JDK.

使用Rx Java,在坚持使用JDK的DirectoryStream的同时,可以通过多种方式来解决需求。

Following combinations will give you the desired effect, I'd explain them in sequence:

下面的组合会给你想要的效果,我将依次解释它们:

Approach 1. A recursive approach using flatMap() and defer() operators

方法1。使用flatMap()和deferred()操作符的递归方法

Approach 2. A recursive approach using flatMap() and fromCallable operators

方法2。使用flatMap()和fromCallable操作符的递归方法

Note: If you replace usage of flatMap() with concatMap(), the directory tree navigation will necessarily happen in a depth-first-search (DFS) manner. With flatMap(), DFS effect is not guaranteed.

注意:如果您用concatMap()替换了flatMap()的用法,那么目录树导航必然以深度优先搜索(DFS)的方式进行。使用flatMap(),无法保证DFS效果。

Approach 1: Using flatMap() and defer()

方法1:使用flatMap()和deferred ()

   private Observable<Path> recursiveFileSystemNavigation_Using_Defer(Path dir) {
       return Observable.<Path>defer(() -> {
            //
            // try-resource block
            //
            try(DirectoryStream<Path> children = Files.newDirectoryStream(dir))
            {
                //This intermediate storage is required because DirectoryStream can't be navigated more than once.
                List<Path> subfolders = Observable.<Path>fromIterable(children)
                                                        .toList()
                                                        .blockingGet();


                return Observable.<Path>fromIterable(subfolders)
                        /* Line X */    .flatMap(p -> !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_Using_Defer(p), Runtime.getRuntime().availableProcessors());

                //      /* Line Y */  .concatMap(p -> !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_Using_Defer(p));

            } catch (IOException e) {
                /*
                 This catch block is required even though DirectoryStream is  Closeable
                 resource. Reason is that .close() call on a DirectoryStream throws a 
                 checked exception.
                */
                return Observable.<Path>empty();
            }
       });
    }

This approach is finding children of given directory and then emitting the children as Observables. If a child is a file, it will be immediately available to a subscriber else flatMap() on Line X will invoke the method recursively passing each sub-directory as argument. For each such subdir, flatmap will internally subscribe to their children all at the same time. This is like a chain-reaction which needs to be controlled.

这种方法是查找给定目录的子目录,然后将子目录作为可观察的对象发送出去。如果子目录是一个文件,那么第X行上的订阅服务器else flatMap()就会立即使用它,并将该方法递归地传递给每个子目录作为参数。对于每个这样的子目录,flatmap将同时在内部订阅它们的子目录。这就像一个需要控制的链式反应。

Therefore use of Runtime.getRuntime().availableProcessors() sets the maximum concurrency level for flatmap() and prevents it from subscribing to all subfolders at the same time. Without setting concurrency level, imagine what will happen when a folder had 1000 children.

因此,使用Runtime.getRuntime(). availableprocessor()为flatmap()设置最大并发级别,并防止它同时订阅所有子文件夹。在不设置并发级别的情况下,想象一下如果一个文件夹有1000个子文件夹会发生什么。

Use of defer() prevents the creation of a DirectoryStream prematurely and ensures it will happen only when a real subscription to find its subfolders is made.

延迟()的使用可以防止DirectoryStream的过早创建,并确保只有在真正订阅了查找其子文件夹时才会发生。

Finally the method returns an Observable < Path > so that a client can subscribe and do something useful with the results as shown below:

最后,该方法返回一个可观察的< Path >,这样客户端可以订阅并做一些有用的事情,如下所示:

//
// Using the defer() based approach
//
recursiveDirNavigation.recursiveFileSystemNavigation_Using_Defer(startingDir)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.from(Executors.newFixedThreadPool(1)))
                    .subscribe(p -> System.out.println(p.toUri()));

Disadvantage of using defer() is that it does not deal with checked exceptions nicely if its argument function is throwing a checked exception. Therefore even though DirectoryStream (which implements Closeable) was created in a try-resource block, we still had to catch the IOException because the auto closure of a DirectoryStream throws that checked exception.

使用deferred()的缺点是,如果它的参数函数抛出一个已检查的异常,它就不能很好地处理已检查的异常。因此,即使DirectoryStream(实现close)是在try-resource块中创建的,我们仍然必须捕获IOException,因为DirectoryStream的自动闭包抛出了已检查的异常。

While using Rx based style, use of catch() blocks for error handling sounds a bit odd because even errors are sent as events in reactive programming. So why not we use an operator which exposes such errors as events.

在使用基于Rx的样式时,使用catch()块处理错误听起来有点奇怪,因为在反应性编程中,甚至错误都作为事件发送。所以我们为什么不使用一个操作符来公开诸如事件之类的错误呢?

A better alternative named as fromCallable() was added in Rx Java 2.x. 2nd approach shows the use of it.

在Rx Java 2.x中添加了一个更好的替代方法fromCallable()。第二种方法展示了它的用途。

Approach 2. Using flatMap() and fromCallable operators

方法2。使用flatMap()和fromCallable操作符。

This approach uses fromCallable() operator which takes a Callable as argument. Since we want a recursive approach, the expected result from that callable is an Observable of children of given folder. Since we want a subscriber to receive results when they are available, we need to return a Observable from this method. Since the result of inner callable is an Observable list of children, the net effect is an Observable of Observables.

这种方法使用fromCallable()操作符,它以Callable作为参数。由于我们希望采用递归方法,因此可调用的预期结果是给定文件夹的子目录的可观察结果。由于我们希望订阅服务器在可用时接收结果,因此需要从该方法返回可观察的结果。由于内部可调用的结果是一个可观察到的子列表,其净效果是可观察到的。

   private Observable<Observable<Path>> recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(Path dir) {
       /*
        * fromCallable() takes a Callable argument. In this case the callbale's return value itself is 
        * a list of sub-paths therefore the overall return value of this method is Observable<Observable<Path>>
        * 
        * While subscribing the final results, we'd flatten this return value.
        * 
        * Benefit of using fromCallable() is that it elegantly catches the checked exceptions thrown 
        * during the callable's call and exposes that via onError() operator chain if you need. 
        * 
        * Defer() operator does not give that flexibility and you have to explicitly catch and handle appropriately.   
        */
       return Observable.<Observable<Path>> fromCallable(() -> traverse(dir))
                                        .onErrorReturnItem(Observable.<Path>empty());

    }

    private Observable<Path> traverse(Path dir) throws IOException {
        //
        // try-resource block
        //
        try(DirectoryStream<Path> children = Files.newDirectoryStream(dir))
        {
            //This intermediate storage is required because DirectoryStream can't be navigated more than once.
            List<Path> subfolders = Observable.<Path>fromIterable(children)
                                                    .toList()
                                                    .blockingGet();

            return Observable.<Path>fromIterable(subfolders)
                    /* Line X */    .flatMap(p -> ( !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(p).blockingSingle())
                                             ,Runtime.getRuntime().availableProcessors());

            //      /* Line Y */  .concatMap(p -> ( !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(p).blockingSingle() ));

        }
    }

A subscriber will then need to flatten the results stream as shown below:

然后,订阅者需要将结果流压平,如下所示:

//
// Using the fromCallable() based approach
//
recursiveDirNavigation.recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(startingDir)
                        .subscribeOn(Schedulers.io())
                        .flatMap(p -> p)
                        .observeOn(Schedulers.from(Executors.newFixedThreadPool(1)))
                        .subscribe(filePath -> System.out.println(filePath.toUri()));

In traverse() method, why is line X using blocking Get

在遍历()方法中,为什么X行使用阻塞Get

Because the recursive function returns an Observable < Observable >, but flatmap at that line needs an Observable to subscribe to.

因为递归函数返回一个可观测的 <可观测的> ,但是该直线上的平面映射需要一个可观测的订阅。

Line Y in both approaches uses concatMap()

两种方法中的第Y行都使用concatMap()

Because concatMap() can be comfortably used if we don't want parallelism during innner subscriptions made by flatmap().

因为如果我们不希望在flatmap()进行innner订阅期间出现并行,那么concatMap()可以很方便地使用。

In both approaches, the implementation of method isFolder looks like below:

在这两种方法中,方法isFolder的实现如下:

private boolean isFolder(Path p){
    if(p.toFile().isFile()){
        return false;
    }

    return true;
}

Maven coordinates for Java RX 2.0

Java RX 2.0的Maven坐标

<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxjava</artifactId>
    <version>2.0.3</version>
</dependency>

Imports in Java file

进口Java文件

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.Executors;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

#6


1  

This is the shortest implementation I came up with:

这是我想到的最短的实现:

final List<Path> files = new ArrayList<>();
Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
try {
    Files.walk(path).forEach(entry -> list.add(entry));
} catch (IOException e) {
    e.printStackTrack();
}

#7


0  

Try this ..it traverses through every folder and print both folder as well as files:-

试试这个. .它遍历每个文件夹并打印两个文件夹和文件:-

public static void traverseDir(Path path) {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                System.out.println("Sub-Folder Name : " + entry.toString());
                traverseDir(entry);
            } else {
                System.out.println("\tFile Name : " + entry.toString());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#8


-1  

Try : You will get a list of directory and sub-directory path; There may be unlimited sub-directory, try to use recursive process.

尝试:您将获得目录和子目录路径的列表;可能有无限子目录,尽量使用递归过程。

public class DriectoryFileFilter {
    private List<String> filePathList = new ArrayList<String>();

    public List<String> read(File file) {
        if (file.isFile()) {
            filePathList.add(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            File[] listOfFiles = file.listFiles();
            if (listOfFiles != null) {
                for (int i = 0; i < listOfFiles.length; i++){
                    read(listOfFiles[i]);
                }
            } else {
                System.out.println("[ACCESS DENIED]");
            }
        }
        return filePathList;
    }
}

#1


35  

Java 8 provides a nice way for that:

Java 8提供了一种很好的方式:

Files.walk(path)

This method returns Stream<Path>.

该方法返回流 <路径> 。

#2


29  

Make a method which will call itself if a next element is directory

创建一个方法,如果下一个元素是目录,它将调用自己

void listFiles(Path path) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                listFiles(entry);
            }
            files.add(entry);
        }
    }
}

#3


24  

Check FileVisitor, very neat.

检查中FileVisitor非常整洁。

 Path path= Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
 final List<Path> files=new ArrayList<>();
 try {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
     @Override
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
          if(!attrs.isDirectory()){
               files.add(file);
          }
          return FileVisitResult.CONTINUE;
      }
     });
 } catch (IOException e) {
      e.printStackTrace();
 }

#4


5  

If you want to avoid having the function calling itself recursively and having a file list that is a member variable, you can use a stack:

如果您想避免函数递归地调用自身,并且有一个作为成员变量的文件列表,您可以使用堆栈:

private List<Path> listFiles(Path path) throws IOException {
    Deque<Path> stack = new ArrayDeque<Path>();
    final List<Path> files = new LinkedList<>();

    stack.push(path);

    while (!stack.isEmpty()) {
        DirectoryStream<Path> stream = Files.newDirectoryStream(stack.pop());
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                stack.push(entry);
            }
            else {
                files.add(entry);
            }
        }
        stream.close();
    }

    return files;
}

#5


2  

Using Rx Java, the requirement can be solved in a number of ways while sticking to usage of DirectoryStream from JDK.

使用Rx Java,在坚持使用JDK的DirectoryStream的同时,可以通过多种方式来解决需求。

Following combinations will give you the desired effect, I'd explain them in sequence:

下面的组合会给你想要的效果,我将依次解释它们:

Approach 1. A recursive approach using flatMap() and defer() operators

方法1。使用flatMap()和deferred()操作符的递归方法

Approach 2. A recursive approach using flatMap() and fromCallable operators

方法2。使用flatMap()和fromCallable操作符的递归方法

Note: If you replace usage of flatMap() with concatMap(), the directory tree navigation will necessarily happen in a depth-first-search (DFS) manner. With flatMap(), DFS effect is not guaranteed.

注意:如果您用concatMap()替换了flatMap()的用法,那么目录树导航必然以深度优先搜索(DFS)的方式进行。使用flatMap(),无法保证DFS效果。

Approach 1: Using flatMap() and defer()

方法1:使用flatMap()和deferred ()

   private Observable<Path> recursiveFileSystemNavigation_Using_Defer(Path dir) {
       return Observable.<Path>defer(() -> {
            //
            // try-resource block
            //
            try(DirectoryStream<Path> children = Files.newDirectoryStream(dir))
            {
                //This intermediate storage is required because DirectoryStream can't be navigated more than once.
                List<Path> subfolders = Observable.<Path>fromIterable(children)
                                                        .toList()
                                                        .blockingGet();


                return Observable.<Path>fromIterable(subfolders)
                        /* Line X */    .flatMap(p -> !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_Using_Defer(p), Runtime.getRuntime().availableProcessors());

                //      /* Line Y */  .concatMap(p -> !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_Using_Defer(p));

            } catch (IOException e) {
                /*
                 This catch block is required even though DirectoryStream is  Closeable
                 resource. Reason is that .close() call on a DirectoryStream throws a 
                 checked exception.
                */
                return Observable.<Path>empty();
            }
       });
    }

This approach is finding children of given directory and then emitting the children as Observables. If a child is a file, it will be immediately available to a subscriber else flatMap() on Line X will invoke the method recursively passing each sub-directory as argument. For each such subdir, flatmap will internally subscribe to their children all at the same time. This is like a chain-reaction which needs to be controlled.

这种方法是查找给定目录的子目录,然后将子目录作为可观察的对象发送出去。如果子目录是一个文件,那么第X行上的订阅服务器else flatMap()就会立即使用它,并将该方法递归地传递给每个子目录作为参数。对于每个这样的子目录,flatmap将同时在内部订阅它们的子目录。这就像一个需要控制的链式反应。

Therefore use of Runtime.getRuntime().availableProcessors() sets the maximum concurrency level for flatmap() and prevents it from subscribing to all subfolders at the same time. Without setting concurrency level, imagine what will happen when a folder had 1000 children.

因此,使用Runtime.getRuntime(). availableprocessor()为flatmap()设置最大并发级别,并防止它同时订阅所有子文件夹。在不设置并发级别的情况下,想象一下如果一个文件夹有1000个子文件夹会发生什么。

Use of defer() prevents the creation of a DirectoryStream prematurely and ensures it will happen only when a real subscription to find its subfolders is made.

延迟()的使用可以防止DirectoryStream的过早创建,并确保只有在真正订阅了查找其子文件夹时才会发生。

Finally the method returns an Observable < Path > so that a client can subscribe and do something useful with the results as shown below:

最后,该方法返回一个可观察的< Path >,这样客户端可以订阅并做一些有用的事情,如下所示:

//
// Using the defer() based approach
//
recursiveDirNavigation.recursiveFileSystemNavigation_Using_Defer(startingDir)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.from(Executors.newFixedThreadPool(1)))
                    .subscribe(p -> System.out.println(p.toUri()));

Disadvantage of using defer() is that it does not deal with checked exceptions nicely if its argument function is throwing a checked exception. Therefore even though DirectoryStream (which implements Closeable) was created in a try-resource block, we still had to catch the IOException because the auto closure of a DirectoryStream throws that checked exception.

使用deferred()的缺点是,如果它的参数函数抛出一个已检查的异常,它就不能很好地处理已检查的异常。因此,即使DirectoryStream(实现close)是在try-resource块中创建的,我们仍然必须捕获IOException,因为DirectoryStream的自动闭包抛出了已检查的异常。

While using Rx based style, use of catch() blocks for error handling sounds a bit odd because even errors are sent as events in reactive programming. So why not we use an operator which exposes such errors as events.

在使用基于Rx的样式时,使用catch()块处理错误听起来有点奇怪,因为在反应性编程中,甚至错误都作为事件发送。所以我们为什么不使用一个操作符来公开诸如事件之类的错误呢?

A better alternative named as fromCallable() was added in Rx Java 2.x. 2nd approach shows the use of it.

在Rx Java 2.x中添加了一个更好的替代方法fromCallable()。第二种方法展示了它的用途。

Approach 2. Using flatMap() and fromCallable operators

方法2。使用flatMap()和fromCallable操作符。

This approach uses fromCallable() operator which takes a Callable as argument. Since we want a recursive approach, the expected result from that callable is an Observable of children of given folder. Since we want a subscriber to receive results when they are available, we need to return a Observable from this method. Since the result of inner callable is an Observable list of children, the net effect is an Observable of Observables.

这种方法使用fromCallable()操作符,它以Callable作为参数。由于我们希望采用递归方法,因此可调用的预期结果是给定文件夹的子目录的可观察结果。由于我们希望订阅服务器在可用时接收结果,因此需要从该方法返回可观察的结果。由于内部可调用的结果是一个可观察到的子列表,其净效果是可观察到的。

   private Observable<Observable<Path>> recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(Path dir) {
       /*
        * fromCallable() takes a Callable argument. In this case the callbale's return value itself is 
        * a list of sub-paths therefore the overall return value of this method is Observable<Observable<Path>>
        * 
        * While subscribing the final results, we'd flatten this return value.
        * 
        * Benefit of using fromCallable() is that it elegantly catches the checked exceptions thrown 
        * during the callable's call and exposes that via onError() operator chain if you need. 
        * 
        * Defer() operator does not give that flexibility and you have to explicitly catch and handle appropriately.   
        */
       return Observable.<Observable<Path>> fromCallable(() -> traverse(dir))
                                        .onErrorReturnItem(Observable.<Path>empty());

    }

    private Observable<Path> traverse(Path dir) throws IOException {
        //
        // try-resource block
        //
        try(DirectoryStream<Path> children = Files.newDirectoryStream(dir))
        {
            //This intermediate storage is required because DirectoryStream can't be navigated more than once.
            List<Path> subfolders = Observable.<Path>fromIterable(children)
                                                    .toList()
                                                    .blockingGet();

            return Observable.<Path>fromIterable(subfolders)
                    /* Line X */    .flatMap(p -> ( !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(p).blockingSingle())
                                             ,Runtime.getRuntime().availableProcessors());

            //      /* Line Y */  .concatMap(p -> ( !isFolder(p) ? Observable.<Path> just(p) : recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(p).blockingSingle() ));

        }
    }

A subscriber will then need to flatten the results stream as shown below:

然后,订阅者需要将结果流压平,如下所示:

//
// Using the fromCallable() based approach
//
recursiveDirNavigation.recursiveFileSystemNavigation_WithoutExplicitCatchBlock_UsingFromCallable(startingDir)
                        .subscribeOn(Schedulers.io())
                        .flatMap(p -> p)
                        .observeOn(Schedulers.from(Executors.newFixedThreadPool(1)))
                        .subscribe(filePath -> System.out.println(filePath.toUri()));

In traverse() method, why is line X using blocking Get

在遍历()方法中,为什么X行使用阻塞Get

Because the recursive function returns an Observable < Observable >, but flatmap at that line needs an Observable to subscribe to.

因为递归函数返回一个可观测的 <可观测的> ,但是该直线上的平面映射需要一个可观测的订阅。

Line Y in both approaches uses concatMap()

两种方法中的第Y行都使用concatMap()

Because concatMap() can be comfortably used if we don't want parallelism during innner subscriptions made by flatmap().

因为如果我们不希望在flatmap()进行innner订阅期间出现并行,那么concatMap()可以很方便地使用。

In both approaches, the implementation of method isFolder looks like below:

在这两种方法中,方法isFolder的实现如下:

private boolean isFolder(Path p){
    if(p.toFile().isFile()){
        return false;
    }

    return true;
}

Maven coordinates for Java RX 2.0

Java RX 2.0的Maven坐标

<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxjava</artifactId>
    <version>2.0.3</version>
</dependency>

Imports in Java file

进口Java文件

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.Executors;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

#6


1  

This is the shortest implementation I came up with:

这是我想到的最短的实现:

final List<Path> files = new ArrayList<>();
Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles");
try {
    Files.walk(path).forEach(entry -> list.add(entry));
} catch (IOException e) {
    e.printStackTrack();
}

#7


0  

Try this ..it traverses through every folder and print both folder as well as files:-

试试这个. .它遍历每个文件夹并打印两个文件夹和文件:-

public static void traverseDir(Path path) {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                System.out.println("Sub-Folder Name : " + entry.toString());
                traverseDir(entry);
            } else {
                System.out.println("\tFile Name : " + entry.toString());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#8


-1  

Try : You will get a list of directory and sub-directory path; There may be unlimited sub-directory, try to use recursive process.

尝试:您将获得目录和子目录路径的列表;可能有无限子目录,尽量使用递归过程。

public class DriectoryFileFilter {
    private List<String> filePathList = new ArrayList<String>();

    public List<String> read(File file) {
        if (file.isFile()) {
            filePathList.add(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            File[] listOfFiles = file.listFiles();
            if (listOfFiles != null) {
                for (int i = 0; i < listOfFiles.length; i++){
                    read(listOfFiles[i]);
                }
            } else {
                System.out.println("[ACCESS DENIED]");
            }
        }
        return filePathList;
    }
}