In  this tutorial, we will see “How to find a String in a Text File using Java 8?” check if text or string present in a file using java 8

Java 8 – How to find a ‘STRING’ in a Text File? – Simplest Examples Share on X

Java 8 1st method:

 

Output:

check if text or string present in a file using java 8

In above example, we are using the lines method of the Files class to create a stream of lines in the file, and after that it uses the anyMatch method to check if any of the lines contain the search word/term. If any line contains the search word, then the anyMatch method will return true, and the search term will be considered as found. But, If none of the lines contain the search word/term, then the anyMatch method will return false, and the search term will be considered as not found.

This can also be achieved using filter method to create a stream that only includes the lines that contain the search term, and after that, use the count method to count the number of lines that contain the search word/term.

Java 8 2nd method:

 

Output:

remove whitespaces from string in java

skip method java 8 stream

Java 8 – How to find a ‘STRING’ in a Text File? – Simplest Examples Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak