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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Find a 'String' in a Text File using Java 8 * @author Deepak Verma * */ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class Find_String_in_Text_File_Java8_Example { public static void main(String[] args) throws IOException { String searchTerm = "Techndeck"; String filePath = "/Users/deepakverma/Documents/SampleFile.txt"; //here you need to specify the location of your text file. In our case, we have a file named 'SampleFile.txt' at the specified location and it contains a word 'Techndeck' try (Stream<String> lines = Files.lines(Paths.get(filePath))) { boolean found = lines.anyMatch(line -> line.contains(searchTerm)); if (found) { System.out.println("Found the search term in the file."); } else { System.out.println("Did not find the search term in the file."); } } } } |
Output:
1 | Found the search term in the file. |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Find a 'String' in a Text File using Java 8 * @author Deepak Verma * */ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class Find_String_in_Text_File_Java8_Example { public static void main(String[] args) throws IOException { String searchTerm = "jlfasdjfjadsfjaldjsf"; String filePath = "/Users/ishadeepak/Documents/SampleFile.txt"; //here you need to specify the location of your text file. In our case, we have a file named 'SampleFile.txt' at the specified location and it does not contain the word 'jlfasdjfjadsfjaldjsf' try (Stream<String> lines = Files.lines(Paths.get(filePath))) { long count = lines.filter(line -> line.contains(searchTerm)).count(); if (count>0) { System.out.println("Found the word in the file"); } else { System.out.println("Did not find the word in the file"); } } } } |
Output:
1 | Did not find the word in the file |
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:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: