Example usage for io.vertx.core.file FileSystem delete

List of usage examples for io.vertx.core.file FileSystem delete

Introduction

In this page you can find the example usage for io.vertx.core.file FileSystem delete.

Prototype

@Fluent
FileSystem delete(String path, Handler<AsyncResult<Void>> handler);

Source Link

Document

Deletes the file represented by the specified path , asynchronously.

Usage

From source file:org.entcore.feeder.csv.CsvImportsLauncher.java

License:Open Source License

private void moveCsvFiles(final String structureExternalId, final FileSystem fs, final String dirName,
        final String parentDir, final Handler<Void>[] handlers, final int j) {
    fs.readDir(dirName, ".*.csv", new Handler<AsyncResult<List<String>>>() {
        @Override//from   ww w.  j  av  a 2  s  .  c  o m
        public void handle(final AsyncResult<List<String>> l) {
            if (l.succeeded()) {
                final int size = l.result().size();
                if (!(size > 0)) {
                    emptyDirectory(fs, parentDir, handlers, j);
                    return;
                }

                final AtomicInteger validFilesCount = new AtomicInteger(size);
                final AtomicInteger count = new AtomicInteger(size);
                for (final String f : l.result()) {
                    String profile = null;
                    for (String profilePattern : profiles.fieldNames()) {
                        if (f.contains(profilePattern)) {
                            profile = profiles.getString(profilePattern);
                            break;
                        }
                    }
                    if (profile == null) {
                        validFilesCount.decrementAndGet();
                        fs.delete(f, new Handler<AsyncResult<Void>>() {
                            @Override
                            public void handle(AsyncResult<Void> event) {
                                if (count.decrementAndGet() == 0) {
                                    importFiles(validFilesCount, parentDir, structureExternalId, handlers, j,
                                            fs);
                                }
                            }
                        });
                        continue;
                    }

                    fs.move(f, dirName + File.separator + profile + ".csv", new Handler<AsyncResult<Void>>() {
                        @Override
                        public void handle(AsyncResult<Void> event2) {
                            if (event2.succeeded()) {
                                if (count.decrementAndGet() == 0) {
                                    importFiles(validFilesCount, parentDir, structureExternalId, handlers, j,
                                            fs);
                                }
                            } else {
                                fs.deleteRecursive(parentDir, true, null);
                                log.error("Error mv csv file : " + f, l.cause());
                                handlers[j + 1].handle(null);
                            }
                        }
                    });
                }
            } else {
                fs.deleteRecursive(parentDir, true, null);
                log.error("Error listing csv in directory : " + dirName, l.cause());
                handlers[j + 1].handle(null);
            }
        }
    });
}