Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io.reactivex.Observable;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.List;
- import java.util.stream.Stream;
- public class Streaming {
- public static void main(String[] args) throws IOException {
- // create Java 8 stream in try-with-resource
- try(Stream<String> stream = Files.lines(Paths.get("data.txt"))) {
- // convert stream into Observable (no data loaded yet)
- Observable<List<String>> buffer = Observable.fromIterable(stream::iterator)
- .buffer(2)
- .filter(s -> true); // do whatever transformations are needed
- // pass reactive stream into writing function (still no data loaded)
- writeToCsv(buffer);
- }
- }
- private static void writeToCsv(Observable<List<String>> buffer) throws IOException {
- FileWriter fw = new FileWriter("out.csv");
- // subscribe to reactive stream -> now do work and write results directly to file!
- buffer.forEach(record -> fw.append("..."));
- }
- }
Add Comment
Please, Sign In to add comment