Search code examples
java-8tokenizejava-io

Java InputStream to Java8 Stream tokenizing


Is there a way to get each word from a text file as a Java8 stream of Strings?

E.g. you have test.txt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod

You get the stream:

BufferedReader br = new BufferedReader(new FileReader("test.txt"));
Stream<String> s = br.lines();

And emit a stream of words (splitting at spaces), that you can .forEach print?

Lorem
ipsum
dolor
sit
amet,
consectetur
adipiscing
elit,
sed
do
eiusmod

Solution

  • Assuming your words are separated by spaces:

        try (Stream<String> s = Files.lines(Paths.get("Your path"))
                .flatMap(Pattern.compile("\\s+")::splitAsStream)) {
            s.forEach(System.out::println);
        }