Created
September 18, 2021 11:40
-
-
Save GaetanoPiazzolla/a8505cf39b1168bd20d2c0f5926d0076 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import reactor.core.publisher.*; | |
import reactor.core.scheduler.Schedulers; | |
public Flux<String> searchReactiveStream() { | |
return Flux.create((FluxSink<String> fluxSink) -> { | |
InputStream inputStream = null; | |
try { | |
inputStream = new FileInputStream(pathFile); | |
} catch (FileNotFoundException e) { | |
fluxSink.error(e); | |
} | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
Stream<String> linesStream = bufferedReader.lines(); | |
AtomicBoolean found = new AtomicBoolean(false); | |
linesStream.forEach(l -> { | |
if (l.contains(SEARCH_TOKEN)) { | |
fluxSink.next(l); | |
found.set(true); | |
} | |
}); | |
if (!found.get()) { | |
fluxSink.next("No results found"); | |
} | |
fluxSink.complete(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
related to medium article https://gae-piaz.medium.com/search-string-in-file-with-java-as-fast-as-possible-fedafdc7a3ee