Search code examples
androidrx-javareadfile

Read Files using Rx Java


I have a number of files from a folder, which I am reading and storing its content to the database. How can I perform the same operation subsequently using RX Java? As I have many files to read and It is taking a lot of time in Async Task

String path = "some path";
        File directory = new File(path);
        File[] files = directory.listFiles();
        Log.d(TAG, "Size: " + files.length);
        for (int i = 0; i < files.length; i++) {
            Log.d(TAG, "FileName -> " + files[i].getName());

         //execute async to read file
        }

Solution

  • You can replace your single threaded approach with multithreading in RxJava insanely easily (in comparison without using RxJava). AsyncTask offers parallel execution using executeOnExecutor(Executor exec, Params... params) so your current implementation could be tweaked for an AsyncTask to process single files rather complete directories. However your question was about RxJava.

    Multithreading will speed up the file reading - speed improvements will depend on quite a few factors, but the main one being your flash memory sequential read / random read speeds. Seeing as this will be a cold Observable their is really no point using a Flowable as back pressure will not come into play here.

    pseudo code for implementing some sort of multithreading :

        Observable.just(directory)
                .flatMap(dir ->
                        Observable.fromArray(dir.listFiles()))
                .filter(File::isFile)
                .flatMap(file ->
                        Observable.just(file)
                                .map(this::readFileFunctionWithReturn)
                                .subscribeOn(Schedulers.io())) // execute on multiple threads
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()) // observe on UI, or choose another Scheduler
                .subscribe(fileResult -> {
                    // unordered results
                });
    

    You can also create custom Scheduler implementation backed by an Executor if you want more fine grained control over the threading i.e custom ThreadFactory.