Skip to content

Instantly share code, notes, and snippets.

@teivah
Created May 30, 2018 13:32
Show Gist options
  • Save teivah/7411b88da984a1fbc8550ca3fcae1ccd to your computer and use it in GitHub Desktop.
Save teivah/7411b88da984a1fbc8550ca3fcae1ccd to your computer and use it in GitHub Desktop.
// Read file method
private Future<String> readFile() {
Future<String> future = Future.future();
// Retrieve a FileSystem object from vertx instance and call the
// non-blocking readFile method
vertx.fileSystem().readFile("src/main/resources/example03/read.txt", handler -> {
if (handler.succeeded()) {
System.out.println("Read content: " + handler.result());
future.complete("read success");
} else {
System.err.println("Error while reading from file: " + handler.cause().getMessage());
future.fail(handler.cause());
}
});
return future;
}
// Write file method
private Future<String> writeFile(String input) {
Future<String> future = Future.future();
String file = "src/main/resources/example03/write.txt";
// Retrieve a FileSystem object from vertx instance and call the
// non-blocking writeFile method
vertx.fileSystem().writeFile(file, Buffer.buffer(input), handler -> {
if (handler.succeeded()) {
System.out.println("File written with " + input);
future.complete(file);
} else {
System.err.println("Error while writing in file: " + handler.cause().getMessage());
}
});
return future;
}
// Write file method
private Future<String> copyFile(String input) {
Future<String> future = Future.future();
// Retrieve a FileSystem object from vertx instance and call the
// non-blocking writeFile method
vertx.fileSystem().copy(input, "src/main/resources/example03/writecopy.txt", handler -> {
if (handler.succeeded()) {
System.out.println("Copy done of " + input);
future.complete("Copy success");
} else {
System.err.println("Error while copying a file: " + handler.cause().getMessage());
future.fail(handler.cause());
}
});
return future;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment