Skip to content

Instantly share code, notes, and snippets.

@8enet
Last active September 7, 2016 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8enet/c232552889822f91e0520e59f385ea9c to your computer and use it in GitHub Desktop.
Save 8enet/c232552889822f91e0520e59f385ea9c to your computer and use it in GitHub Desktop.
rxjava

使用rxjava递归遍历目录和统计目录所有文件大小

public static Observable<File> getFile(File file){
        if(file.isFile()){
            return Observable.just(file);
        }
        return Observable.from(file.listFiles()).concatMap(new Func1<File, Observable<File>>() {
            @Override
            public Observable<File> call(File file) {
                return getFile(file);
            }
        });
    }

public static void main(String[] args){
        File file=new File("/Users/zl/Downloads");
        getFile(file).subscribe(new Action1<File>() {
            @Override
            public void call(File file) {
                System.out.println(file);
            }
        });

        getFile(file).collect(new Func0<AtomicLong>() {
            @Override
            public AtomicLong call() {
                return new AtomicLong(0);
            }
        }, new Action2<AtomicLong, File>() {
            @Override
            public void call(AtomicLong aLong, File file) {
                aLong.getAndAdd(file.length());
            }
        }).subscribe(new Action1<AtomicLong>() {
            @Override
            public void call(AtomicLong atomicLong) {
                System.out.println(atomicLong);
            }
        });
        
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment